//! Edit task screen - Modify existing task description
//!
//! Similar to create_task but pre-populated with existing task data

// 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, Paragraph},
    Frame,
};

/// Render edit task screen
pub(crate) fn render_edit_task_screen(f: &mut Frame<'_>, app: &App, area: Rect, theme: &AppTheme) {
    // Create layout
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(10),   // Task input area
            Constraint::Length(5), // Instructions
            Constraint::Length(3), // Error display
        ])
        .split(area);

    // Render header
    let task_id = app
        .editing_task_id
        .as_ref()
        .map(|id| format!(" (ID: {})", id))
        .unwrap_or_default();

    let header = Paragraph::new(vec![Line::from(vec![Span::styled(
        format!("✏️  Edit Task{}", task_id),
        theme.header_style(),
    )])])
    .block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(theme.border_style()),
    )
    .alignment(Alignment::Center);
    f.render_widget(header, chunks[0]);

    // Render textarea
    let textarea_block = Block::default()
        .borders(Borders::ALL)
        .border_style(theme.border_style())
        .title(" Task Description ");

    let inner_area = textarea_block.inner(chunks[1]);
    f.render_widget(textarea_block, chunks[1]);

    // Render textarea content
    let lines: Vec<Line<'_>> = app
        .task_textarea
        .lines()
        .iter()
        .map(|line| Line::from(line.clone()))
        .collect();

    let content = Paragraph::new(lines)
        .style(theme.list_item_style())
        .alignment(Alignment::Left);
    f.render_widget(content, inner_area);

    // Render instructions
    let instructions = vec![
        Line::from(vec![
            Span::styled(
                "Ctrl+Enter",
                theme.success_style().add_modifier(Modifier::BOLD),
            ),
            Span::raw(" Save Changes  "),
            Span::styled("Esc", theme.warning_style().add_modifier(Modifier::BOLD)),
            Span::raw(" Cancel  "),
        ]),
        Line::from(vec![
            Span::styled("Enter", theme.metadata_style()),
            Span::raw(" New Line  "),
            Span::styled("Backspace", theme.metadata_style()),
            Span::raw(" Delete Character"),
        ]),
    ];

    let instructions_widget = Paragraph::new(instructions)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(theme.border_style())
                .title(" Controls "),
        )
        .alignment(Alignment::Center);
    f.render_widget(instructions_widget, chunks[2]);

    // Render error if present
    if let Some(error) = &app.task_form_error {
        let error_text = vec![Line::from(vec![Span::styled(
            format!("⚠ {}", error),
            theme.error_style(),
        )])];

        let error_widget = Paragraph::new(error_text)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(theme.error_style()),
            )
            .alignment(Alignment::Center);
        f.render_widget(error_widget, chunks[3]);
    } else {
        // Show character count hint
        let char_count = app.task_textarea.lines().join("\n").len();
        let hint_text = vec![Line::from(vec![Span::styled(
            format!("Characters: {} / 500", char_count),
            if char_count > 500 {
                theme.warning_style()
            } else {
                theme.metadata_style()
            },
        )])];

        let hint_widget = Paragraph::new(hint_text)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(theme.border_style()),
            )
            .alignment(Alignment::Center);
        f.render_widget(hint_widget, chunks[3]);
    }
}

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