"use client";
import { useState,type ReactNode } from 'react';
import { ChevronLeft,ChevronRight,ChevronsLeft,ChevronsRight,Search } from 'lucide-react';
import { Card,CardHeader,CardTitle,CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import SearchableSelect from '@/components/ui/searchable-select';
import { Dialog,DialogContent,DialogHeader,DialogTitle,DialogDescription,DialogFooter } from '@/components/ui/dialog';
export const primary='bg-[#1E1B4B] text-white hover:bg-[#2A2675]';
export function Frame({title,description,actions,children}:{title:string;description:string;actions?:ReactNode;children:ReactNode}){return <div className="p-3"><Card className="border border-gray-100 shadow-sm"><CardHeader className="border-b"><div className="flex flex-col justify-between gap-4 md:flex-row md:items-center"><div><CardTitle className="text-2xl font-semibold text-gray-900">{title}</CardTitle><p className="mt-1 text-sm text-gray-500">{description}</p></div><div className="flex flex-wrap items-center gap-2">{actions}</div></div></CardHeader><CardContent className="space-y-4 p-6">{children}</CardContent></Card></div>;}
export function Notice({children}:{children:ReactNode}){return <div role="note" className="rounded-lg border border-amber-100 bg-amber-50/70 px-4 py-3 text-xs leading-5 text-amber-900">{children}</div>;}
export function Modal({title,description,children,footer,onClose,busy=false}:{title:string;description:string;children:ReactNode;footer:ReactNode;onClose:()=>void;busy?:boolean}){return <Dialog open onOpenChange={open=>{if(!open&&!busy)onClose();}}><DialogContent className="flex max-h-[90vh] flex-col gap-0 overflow-hidden p-0 sm:max-w-3xl"><DialogHeader className="shrink-0 border-b bg-gray-50/70 px-6 py-4"><DialogTitle>{title}</DialogTitle><DialogDescription>{description}</DialogDescription></DialogHeader><div className="min-h-0 space-y-4 overflow-y-auto px-6 py-5">{children}</div><DialogFooter className="border-t bg-gray-50/70 px-6 py-4">{footer}</DialogFooter></DialogContent></Dialog>;}
export function Stat({label,value,sub}:{label:string;value:ReactNode;sub?:string}){return <div className="rounded-xl border border-gray-200 bg-white p-4"><p className="text-xs font-medium text-gray-500">{label}</p><p className="mt-2 text-2xl font-semibold tracking-tight text-[#1E1B4B]">{value}</p>{sub&&<p className="mt-1 text-xs text-gray-400">{sub}</p>}</div>;}
export function SearchBox({value,onChange,placeholder='Search records…'}:{value:string;onChange:(value:string)=>void;placeholder?:string}){return <div className="relative min-w-[240px] flex-1"><Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" /><Input aria-label={placeholder} value={value} onChange={e=>onChange(e.target.value)} placeholder={placeholder} className="pl-10" /></div>;}
export function Table({headers,children,empty=false}:{headers:string[];children:ReactNode;empty?:boolean}){return <div className="overflow-x-auto rounded-lg border border-gray-200"><table className="w-full min-w-[800px]"><thead className="border-b border-gray-200 bg-gray-50"><tr>{headers.map(h=><th key={h} scope="col" className={`px-6 py-3 text-xs font-semibold uppercase tracking-wider text-gray-700 ${h==='Actions'?'text-right':'text-left'}`}>{h}</th>)}</tr></thead><tbody className="divide-y divide-gray-100">{empty?<tr><td colSpan={headers.length} className="px-6 py-12 text-center text-sm text-gray-500">No records found.</td></tr>:children}</tbody></table></div>;}
export const cell='px-6 py-4 text-sm text-gray-600';
export function Status({value}:{value:string}){const tone=/^(confirmed|approved|applied|completed|active|valid|saved)/i.test(value)?'bg-emerald-50 text-emerald-700':/rejected|invalid|error/i.test(value)?'bg-rose-50 text-rose-700':'bg-amber-50 text-amber-800';return <span className={`inline-flex whitespace-nowrap rounded-full px-2.5 py-1 text-xs font-medium ${tone}`}>{value}</span>;}
export function usePagination<T>(rows:T[],initial=10){const [page,setPage]=useState(1);const [size,setSize]=useState(initial);const pages=Math.max(1,Math.ceil(rows.length/size));const current=Math.min(page,pages);const first=rows.length?(current-1)*size+1:0;return {rows:rows.slice((current-1)*size,current*size),page:current,pages,size,first,last:Math.min(current*size,rows.length),total:rows.length,setPage,setSize:(value:number)=>{setSize(value);setPage(1);}};}
type PaginationState={page:number;pages:number;size:number;first:number;last:number;total:number;setPage:(value:number)=>void;setSize:(value:number)=>void};
export function EntryCount({state}:{state:PaginationState}){return <div className="ml-auto flex items-center gap-2 text-sm text-gray-600"><span>Show</span><SearchableSelect aria-label="Entries per page" allowAll={false} widthClass="w-28" value={String(state.size)} options={[...new Set([10,15,25,50,100,state.size])].sort((a,b)=>a-b).map(n=>({value:String(n),label:String(n)}))} onChange={value=>state.setSize(Number(value))} /><span>entries</span></div>;}
export function Count({state}:{state:PaginationState}){return <div className="rounded-lg border border-blue-100 bg-blue-50 px-4 py-3 text-sm text-gray-700">Showing {state.first} to {state.last} of {state.total.toLocaleString()} entries</div>;}
export function Pager({state}:{state:PaginationState}){const numbers=Array.from({length:Math.min(5,state.pages)},(_,i)=>Math.max(1,Math.min(state.page-2,state.pages-4))+i);return <footer className="flex flex-col items-center justify-between gap-4 border-t border-gray-200 bg-gray-50 px-6 py-4 text-sm text-gray-600 md:flex-row"><span>Page <strong>{state.page}</strong> of <strong>{state.pages}</strong></span><nav aria-label="Table pagination" className="flex flex-wrap justify-center gap-2">{[{label:'First page',value:1,icon:ChevronsLeft},{label:'Previous page',value:state.page-1,icon:ChevronLeft}].map(x=><Button key={x.label} size="sm" variant="outline" aria-label={x.label} disabled={state.page<=1} onClick={()=>state.setPage(x.value)}><x.icon className="h-4 w-4" /></Button>)}{numbers.map(n=><Button key={n} size="sm" variant={n===state.page?'default':'outline'} className={n===state.page?primary:''} aria-current={n===state.page?'page':undefined} onClick={()=>state.setPage(n)}>{n}</Button>)}{[{label:'Next page',value:state.page+1,icon:ChevronRight},{label:'Last page',value:state.pages,icon:ChevronsRight}].map(x=><Button key={x.label} size="sm" variant="outline" aria-label={x.label} disabled={state.page>=state.pages} onClick={()=>state.setPage(x.value)}><x.icon className="h-4 w-4" /></Button>)}</nav><span>{state.first} – {state.last} of {state.total} entries</span></footer>;}
