use calendar_models::{VAlarm, AlarmAction, AlarmTrigger};
use chrono::Duration;
use yew::prelude::*;

#[derive(Properties, PartialEq)]
pub struct AlarmListProps {
    pub alarms: Vec<VAlarm>,
    pub on_alarm_delete: Callback<usize>, // Index of alarm to delete
    pub on_alarm_edit: Callback<usize>,   // Index of alarm to edit
}

#[function_component(AlarmList)]
pub fn alarm_list(props: &AlarmListProps) -> Html {
    if props.alarms.is_empty() {
        return html! {
            <div class="alarm-list-empty">
                <p class="text-muted">{"No reminders set"}</p>
                <p class="text-small">{"Click 'Add Reminder' to create your first reminder"}</p>
            </div>
        };
    }

    html! {
        <div class="alarm-list">
            <h6>{"Configured Reminders"}</h6>
            <div class="alarm-items">
                {
                    props.alarms.iter().enumerate().map(|(index, alarm)| {
                        let alarm_description = format_alarm_description(alarm);
                        let action_icon = get_action_icon(&alarm.action);
                        
                        let on_delete = {
                            let on_alarm_delete = props.on_alarm_delete.clone();
                            Callback::from(move |_| {
                                on_alarm_delete.emit(index);
                            })
                        };
                        
                        let on_edit = {
                            let on_alarm_edit = props.on_alarm_edit.clone();
                            Callback::from(move |_| {
                                on_alarm_edit.emit(index);
                            })
                        };

                        html! {
                            <div key={index} class="alarm-item">
                                <div class="alarm-content">
                                    <span class="alarm-icon">{action_icon}</span>
                                    <span class="alarm-description">{alarm_description}</span>
                                </div>
                                <div class="alarm-actions">
                                    <button 
                                        class="alarm-action-btn edit-btn" 
                                        title="Edit reminder"
                                        onclick={on_edit}
                                    >
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button 
                                        class="alarm-action-btn delete-btn" 
                                        title="Delete reminder"
                                        onclick={on_delete}
                                    >
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </div>
                        }
                    }).collect::<Html>()
                }
            </div>
        </div>
    }
}

/// Format alarm description for display
fn format_alarm_description(alarm: &VAlarm) -> String {
    match &alarm.trigger {
        AlarmTrigger::Duration(duration) => {
            format_duration_description(duration)
        }
        AlarmTrigger::DateTime(datetime) => {
            format!("At {}", datetime.format("%Y-%m-%d %H:%M UTC"))
        }
    }
}

/// Get icon for alarm action - always use bell for consistent notification type
fn get_action_icon(_action: &AlarmAction) -> Html {
    html! { <i class="fas fa-bell"></i> }
}

/// Format duration for human-readable description
fn format_duration_description(duration: &Duration) -> String {
    let minutes = duration.num_minutes();
    
    if minutes == 0 {
        return "At event time".to_string();
    }
    
    let abs_minutes = minutes.abs();
    let before_or_after = if minutes < 0 { "before" } else { "after" };
    
    // Convert to human-readable format
    if abs_minutes >= 60 * 24 * 7 {
        let weeks = abs_minutes / (60 * 24 * 7);
        let remainder = abs_minutes % (60 * 24 * 7);
        if remainder == 0 {
            format!("{} week{} {}", weeks, if weeks == 1 { "" } else { "s" }, before_or_after)
        } else {
            format!("{} minutes {}", abs_minutes, before_or_after)
        }
    } else if abs_minutes >= 60 * 24 {
        let days = abs_minutes / (60 * 24);
        let remainder = abs_minutes % (60 * 24);
        if remainder == 0 {
            format!("{} day{} {}", days, if days == 1 { "" } else { "s" }, before_or_after)
        } else {
            format!("{} minutes {}", abs_minutes, before_or_after)
        }
    } else if abs_minutes >= 60 {
        let hours = abs_minutes / 60;
        let remainder = abs_minutes % 60;
        if remainder == 0 {
            format!("{} hour{} {}", hours, if hours == 1 { "" } else { "s" }, before_or_after)
        } else {
            format!("{} minutes {}", abs_minutes, before_or_after)
        }
    } else {
        format!("{} minute{} {}", abs_minutes, if abs_minutes == 1 { "" } else { "s" }, before_or_after)
    }
}