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

/// Helper to generate visible page numbers with ellipsis
fn get_page_numbers(current: usize, total: usize) -> Vec<PageItem> {
    if total <= 7 {
        // Show all pages if 7 or fewer
        (1..=total).map(PageItem::Page).collect()
    } else {
        let mut pages = Vec::new();
        pages.push(PageItem::Page(1));

        if current <= 3 {
            // Near start: 1 2 3 4 5 ... 10
            for i in 2..=5.min(total - 1) {
                pages.push(PageItem::Page(i));
            }
            pages.push(PageItem::Ellipsis);
        } else if current >= total - 2 {
            // Near end: 1 ... 6 7 8 9 10
            pages.push(PageItem::Ellipsis);
            for i in (total - 4).max(2)..total {
                pages.push(PageItem::Page(i));
            }
        } else {
            // Middle: 1 ... 4 5 6 ... 10
            pages.push(PageItem::Ellipsis);
            for i in current - 1..=current + 1 {
                pages.push(PageItem::Page(i));
            }
            pages.push(PageItem::Ellipsis);
        }

        pages.push(PageItem::Page(total));
        pages
    }
}

#[derive(Clone, PartialEq)]
enum PageItem {
    Page(usize),
    Ellipsis,
}

#[component]
pub fn PaginationClient(
    current_page: usize,
    total_pages: usize,
    on_page_change: Callback<usize>,
    class: &'static str,
) -> impl IntoView {
    let page_numbers = get_page_numbers(current_page, total_pages);

    let handle_prev = move |_: ev::MouseEvent| {
        if current_page > 1 {
            on_page_change.run(current_page - 1);
        }
    };

    let handle_next = move |_: ev::MouseEvent| {
        if current_page < total_pages {
            on_page_change.run(current_page + 1);
        }
    };

    view! {
        <div class={format!("flex items-center gap-2 {}", class)}>
            <button
                class="px-3 py-2 ds-btn ds-btn-sm bg-white/5 hover:bg-white/8 disabled:opacity-50"
                disabled={current_page <= 1}
                on:click=handle_prev
            >
                "←"
            </button>

            {page_numbers.into_iter().map(|item| {
                match item {
                    PageItem::Page(page) => {
                        let is_current = page == current_page;
                        let button_class = if is_current {
                            "px-3 py-2 ds-btn ds-btn-sm gradient-primary text-white"
                        } else {
                            "px-3 py-2 ds-btn ds-btn-sm bg-white/5 hover:bg-white/8"
                        };

                        let handle_click = move |_: ev::MouseEvent| {
                            on_page_change.run(page);
                        };

                        view! {
                            <button
                                class=button_class
                                on:click=handle_click
                            >
                                {page.to_string()}
                            </button>
                        }.into_any()
                    }
                    PageItem::Ellipsis => {
                        view! {
                            <span class="px-2 text-white/60">"..."</span>
                        }.into_any()
                    }
                }
            }).collect::<Vec<_>>()}

            <button
                class="px-3 py-2 ds-btn ds-btn-sm bg-white/5 hover:bg-white/8 disabled:opacity-50"
                disabled={current_page >= total_pages}
                on:click=handle_next
            >
                "→"
            </button>
        </div>
    }
}
