use dioxus::prelude::*;
use dioxus_free_icons::icons::bs_icons::*;
use dioxus_free_icons::Icon;

use crate::app::Route;
use crate::components::page_header::PageHeader;
use crate::infrastructure::dast::{fetch_dast_findings, fetch_dast_scan_runs};

#[component]
pub fn DastOverviewPage() -> Element {
    let scan_runs = use_resource(|| async { fetch_dast_scan_runs().await.ok() });
    let findings = use_resource(|| async { fetch_dast_findings().await.ok() });

    rsx! {
        PageHeader {
            title: "DAST Overview",
            description: "Dynamic Application Security Testing — scan running applications for vulnerabilities",
        }

        div { class: "stat-cards", style: "margin-bottom: 24px;",
            div { class: "stat-card-item",
                div { class: "stat-card-value",
                    match &*scan_runs.read() {
                        Some(Some(data)) => {
                            let count = data.total.unwrap_or(0);
                            rsx! { "{count}" }
                        },
                        _ => rsx! { "—" },
                    }
                }
                div { class: "stat-card-label",
                    Icon { icon: BsPlayCircle, width: 14, height: 14 }
                    " Total Scans"
                }
            }
            div { class: "stat-card-item",
                div { class: "stat-card-value",
                    match &*findings.read() {
                        Some(Some(data)) => {
                            let count = data.total.unwrap_or(0);
                            rsx! { "{count}" }
                        },
                        _ => rsx! { "—" },
                    }
                }
                div { class: "stat-card-label",
                    Icon { icon: BsShieldExclamation, width: 14, height: 14 }
                    " DAST Findings"
                }
            }
            div { class: "stat-card-item",
                div { class: "stat-card-value", "—" }
                div { class: "stat-card-label",
                    Icon { icon: BsBullseye, width: 14, height: 14 }
                    " Active Targets"
                }
            }
        }

        div { style: "display: flex; gap: 12px; margin-bottom: 24px;",
            Link {
                to: Route::DastTargetsPage {},
                class: "btn btn-primary",
                Icon { icon: BsBullseye, width: 14, height: 14 }
                " Manage Targets"
            }
            Link {
                to: Route::DastFindingsPage {},
                class: "btn btn-secondary",
                Icon { icon: BsShieldExclamation, width: 14, height: 14 }
                " View Findings"
            }
        }

        div { class: "card",
            div { class: "card-header", "Recent Scan Runs" }
            match &*scan_runs.read() {
                Some(Some(data)) => {
                    let runs = &data.data;
                    if runs.is_empty() {
                        rsx! { p { "No scan runs yet." } }
                    } else {
                        rsx! {
                            table { class: "table",
                                thead {
                                    tr {
                                        th { "Target" }
                                        th { "Status" }
                                        th { "Phase" }
                                        th { "Findings" }
                                        th { "Exploitable" }
                                        th { "Started" }
                                    }
                                }
                                tbody {
                                    for run in runs {
                                        tr {
                                            td { "{run.get(\"target_id\").and_then(|v| v.as_str()).unwrap_or(\"-\")}" }
                                            td {
                                                span { class: "badge",
                                                    "{run.get(\"status\").and_then(|v| v.as_str()).unwrap_or(\"unknown\")}"
                                                }
                                            }
                                            td { "{run.get(\"current_phase\").and_then(|v| v.as_str()).unwrap_or(\"-\")}" }
                                            td { "{run.get(\"findings_count\").and_then(|v| v.as_u64()).unwrap_or(0)}" }
                                            td { "{run.get(\"exploitable_count\").and_then(|v| v.as_u64()).unwrap_or(0)}" }
                                            td { "{run.get(\"started_at\").and_then(|v| v.as_str()).unwrap_or(\"-\")}" }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                Some(None) => rsx! { p { "Failed to load scan runs." } },
                None => rsx! { p { "Loading..." } },
            }
        }
    }
}
