use std::collections::HashMap;
use std::sync::Arc;

use chrono::{DateTime, Duration, Utc};
use leptos::prelude::*;
use serde::{Deserialize, Serialize};

// Filtering and Date Range Components

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DateRange {
    pub start: DateTime<Utc>,
    pub end: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DateRangePreset {
    LastHour,
    Last6Hours,
    Last24Hours,
    Last7Days,
    Last30Days,
    ThisWeek,
    ThisMonth,
    LastMonth,
    Custom,
}

impl DateRangePreset {
    pub fn to_date_range(&self) -> DateRange {
        let now = Utc::now();
        match self {
            DateRangePreset::LastHour => DateRange {
                start: now - Duration::hours(1),
                end: now,
            },
            DateRangePreset::Last6Hours => DateRange {
                start: now - Duration::hours(6),
                end: now,
            },
            DateRangePreset::Last24Hours => DateRange {
                start: now - Duration::days(1),
                end: now,
            },
            DateRangePreset::Last7Days => DateRange {
                start: now - Duration::days(7),
                end: now,
            },
            DateRangePreset::Last30Days => DateRange {
                start: now - Duration::days(30),
                end: now,
            },
            DateRangePreset::ThisWeek => DateRange {
                start: now - Duration::days(7),
                end: now,
            },
            DateRangePreset::ThisMonth => DateRange {
                start: now - Duration::days(30),
                end: now,
            },
            DateRangePreset::LastMonth => DateRange {
                start: now - Duration::days(60),
                end: now - Duration::days(30),
            },
            DateRangePreset::Custom => DateRange {
                start: now - Duration::days(1),
                end: now,
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct MetricFilter {
    pub metric_types: Vec<String>,
    pub severity_levels: Vec<String>,
    pub sources: Vec<String>,
    pub users: Vec<String>,
    pub date_range: Option<DateRange>,
    pub custom_filters: HashMap<String, String>,
}

#[derive(Debug, Clone, Default)]
pub struct FilterOptions {
    pub metric_types: Vec<String>,
    pub severity_levels: Vec<String>,
    pub sources: Vec<String>,
    pub users: Vec<String>,
}

// Simplified placeholder components
#[component]
pub fn MetricFilterPanel(
    _filter: ReadSignal<MetricFilter>,
    on_filter_change: Arc<dyn Fn(MetricFilter) + 'static + Send + Sync>,
    #[prop(optional)] _available_options: Option<FilterOptions>,
) -> impl IntoView {
    let (is_expanded, set_is_expanded) = signal(false);
    let filter_handler = Arc::clone(&on_filter_change);

    view! {
        <div class="metric-filter-panel">
            <div class="filter-header">
                <button
                    class="filter-toggle"
                    on:click=move |_| set_is_expanded.update(|exp| *exp = !*exp)
                >
                    <span>"Filters"</span>
                </button>
            </div>
            <Show when=move || is_expanded.get()>
                <div class="filter-content">
                    <p>"Filter components placeholder"</p>
                    <button
                        class="btn-secondary"
                        on:click={
                            let handler = Arc::clone(&filter_handler);
                            move |_| {
                                handler(MetricFilter::default());
                            }
                        }
                    >
                        "Reset All"
                    </button>
                </div>
            </Show>
        </div>
    }
}

#[component]
pub fn DateRangePicker(
    _selected: impl Fn() -> Option<DateRange> + 'static,
    _on_change: Arc<dyn Fn(Option<DateRange>) + 'static + Send + Sync>,
) -> impl IntoView {
    view! {
        <div class="date-range-picker">
            <p>"Date range picker placeholder"</p>
        </div>
    }
}

// Other utility components as placeholders
#[component]
pub fn ErrorMessage(message: String) -> impl IntoView {
    view! {
        <div class="error-message">
            <p>{message}</p>
        </div>
    }
}

#[component]
pub fn SuccessMessage(message: String) -> impl IntoView {
    view! {
        <div class="success-message">
            <p>{message}</p>
        </div>
    }
}
