//! Delete confirmation modal
//!
//! Displays a centered confirmation dialog for task deletion

// Note: This is a binary module
use crate::app::App;
use crate::theme::AppTheme;
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::Modifier,
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

/// Render delete confirmation modal
pub(crate) fn render_delete_confirm_modal(
    f: &mut Frame<'_>,
    app: &App,
    area: Rect,
    theme: &AppTheme,
) {
    // Create centered modal area
    let modal_width = 60;
    let modal_height = 10;

    let modal_area = Rect {
        x: area.x + (area.width.saturating_sub(modal_width)) / 2,
        y: area.y + (area.height.saturating_sub(modal_height)) / 2,
        width: modal_width,
        height: modal_height,
    };

    // Clear background
    f.render_widget(Clear, modal_area);

    // Create modal layout
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(4),    // Message
            Constraint::Length(3), // Controls
        ])
        .split(modal_area);

    // Render modal header
    let header = Paragraph::new(vec![Line::from(vec![Span::styled(
        "⚠️  Confirm Deletion",
        theme.error_style().add_modifier(Modifier::BOLD),
    )])])
    .block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(theme.error_style()),
    )
    .alignment(Alignment::Center);
    f.render_widget(header, chunks[0]);

    // Get task being deleted
    let task_desc = app
        .selected_task()
        .map(|t| {
            let desc = if t.description.len() > 40 {
                format!("{}...", &t.description[..40])
            } else {
                t.description.clone()
            };
            desc
        })
        .unwrap_or_else(|| "Unknown task".to_string());

    // Render confirmation message
    let message = vec![
        Line::from(""),
        Line::from(vec![Span::styled(
            "Are you sure you want to delete this task?",
            theme.list_item_style(),
        )]),
        Line::from(""),
        Line::from(vec![
            Span::styled("Task: ", theme.metadata_style()),
            Span::styled(
                task_desc,
                theme.list_item_style().add_modifier(Modifier::ITALIC),
            ),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "This action cannot be undone.",
            theme.warning_style(),
        )]),
    ];

    let message_widget = Paragraph::new(message)
        .block(
            Block::default()
                .borders(Borders::LEFT | Borders::RIGHT)
                .border_style(theme.error_style()),
        )
        .alignment(Alignment::Center);
    f.render_widget(message_widget, chunks[1]);

    // Render controls
    let controls = vec![Line::from(vec![
        Span::styled("y, Enter", theme.error_style().add_modifier(Modifier::BOLD)),
        Span::raw(" Delete  "),
        Span::styled("n, Esc", theme.success_style().add_modifier(Modifier::BOLD)),
        Span::raw(" Cancel"),
    ])];

    let controls_widget = Paragraph::new(controls)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(theme.error_style()),
        )
        .alignment(Alignment::Center);
    f.render_widget(controls_widget, chunks[2]);
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_delete_confirm_modal_module_exists() {
        // Module compiles successfully
        // This is a placeholder test - actual rendering tests would require full terminal setup
    }
}
