use leptos::prelude::*;
use leptos::ev;

#[cfg(not(target_arch = "wasm32"))]
use super::ssr::TableSSR;

#[cfg(target_arch = "wasm32")]
use super::client::TableClient;

/// Column definition for table
#[derive(Clone, Debug)]
pub struct TableColumn {
    /// Column header text
    pub header: String,
    /// Column key for sorting
    pub key: String,
    /// Whether column is sortable
    pub sortable: bool,
}

impl TableColumn {
    pub fn new(header: impl Into<String>, key: impl Into<String>) -> Self {
        Self {
            header: header.into(),
            key: key.into(),
            sortable: false,
        }
    }

    pub fn sortable(mut self) -> Self {
        self.sortable = true;
        self
    }
}

/// Table component
///
/// Displays data in a tabular format with optional sorting.
///
/// # Examples
///
/// ```rust
/// use leptos::prelude::*;
/// use vapora_leptos_ui::{Table, TableColumn};
///
/// #[component]
/// fn UserList() -> impl IntoView {
///     let columns = vec![
///         TableColumn::new("Name", "name").sortable(),
///         TableColumn::new("Email", "email"),
///         TableColumn::new("Status", "status"),
///     ];
///
///     let rows = vec![
///         vec!["Alice".into(), "alice@example.com".into(), "Active".into()],
///         vec!["Bob".into(), "bob@example.com".into(), "Inactive".into()],
///     ];
///
///     view! {
///         <Table columns=columns rows=rows />
///     }
/// }
/// ```
#[component]
pub fn Table(
    /// Column definitions
    columns: Vec<TableColumn>,
    /// Table data rows (each row is a vec of cell content)
    rows: Vec<Vec<String>>,
    /// Optional callback when column is sorted
    #[prop(optional)]
    #[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))]
    on_sort: Option<Callback<String>>,
    /// Additional CSS classes
    #[prop(default = "")]
    class: &'static str,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    return view! {
        <TableSSR
            columns=columns
            rows=rows
            class=class
        />
    };

    #[cfg(target_arch = "wasm32")]
    return view! {
        <TableClient
            columns=columns
            rows=rows
            on_sort=on_sort.unwrap_or(Callback::new(|_| {}))
            class=class
        />
    };
}
