use iced::{
    Background,
    Element,
    Length,
    widget::{
        button,
        column,
        container,
        scrollable,
        text,
    },
};

use crate::{
    app::{
        Editor,
        Message,
    },
    theme::*,
};

pub fn view(editor: &Editor) -> Element<'_, Message> {
    let title = text("Quick Actions").size(16).color(Neutral::CREAM);

    let has_project = editor.project.is_some();

    let validate_btn = button(text("✓ Validate").size(14).color(if has_project {
        Gold::_500
    } else {
        Neutral::WARM_GRAY_700
    }))
    .padding([Spacing::MICRO, Spacing::SMALL])
    .style(move |_theme, status| button::Style {
        background: match status {
            | button::Status::Hovered if has_project => Some(Background::Color(Aubergine::_700)),
            | _ => None,
        },
        text_color: if has_project {
            Gold::_500
        } else {
            Neutral::WARM_GRAY_700
        },
        ..Default::default()
    });

    let validate_btn = if has_project {
        validate_btn.on_press(Message::ValidateProject)
    } else {
        validate_btn
    };

    let new_char_btn = button(text("⊕ New Character").size(14).color(Gold::_500))
        .on_press(Message::NewCharacter)
        .padding([Spacing::MICRO, Spacing::SMALL])
        .style(|_theme, status| button::Style {
            background: match status {
                | button::Status::Hovered => Some(Background::Color(Aubergine::_700)),
                | _ => None,
            },
            text_color: Gold::_500,
            ..Default::default()
        });

    let actions = column![
        validate_btn,
        new_char_btn,
        text("⊕ New Relationship")
            .size(14)
            .color(Neutral::WARM_GRAY_400),
        text("⊕ New Location")
            .size(14)
            .color(Neutral::WARM_GRAY_400),
        text("🔍 Search...").size(14).color(Neutral::WARM_GRAY_400),
    ]
    .spacing(Spacing::SMALL);

    let project_info = if let Some(ref project) = editor.project {
        column![
            text("Project").size(16).color(Neutral::CREAM),
            text(format!("{} characters", project.characters().count()))
                .size(12)
                .color(Neutral::WARM_GRAY_400),
            text(format!("{} relationships", project.relationships().count()))
                .size(12)
                .color(Neutral::WARM_GRAY_400),
        ]
        .spacing(Spacing::MICRO)
    } else {
        column![text("No project").size(14).color(Neutral::WARM_GRAY_400),].spacing(Spacing::MICRO)
    };

    let content = column![title, actions, project_info]
        .spacing(Spacing::LARGE)
        .padding(Spacing::LARGE);

    container(scrollable(content))
        .width(240) // Fixed width from design spec
        .height(Length::Fill)
        .style(|_theme| container::Style {
            background: Some(panel_background()),
            border: subtle_border(),
            ..Default::default()
        })
        .into()
}
