import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { supabase } from '@/lib/supabase'
import { LogoutButton } from '@/components/logout-button'
import { ArrowLeft, ClipboardList } from 'lucide-react'
import Link from 'next/link'
import { KelolaDataTable } from './KelolaDataTable'
import { CetakInstanModal } from './CetakInstanModal'

export default async function KelolaDataPage() {
    const cookieStore = await cookies()
    const sessionCookie = cookieStore.get('user_session')
    if (!sessionCookie) redirect('/')

    const session = JSON.parse(sessionCookie.value)
    if (session.role !== 'admin') redirect('/dashboard')

    const { data, error } = await supabase
        .from('akun_balita')
        .select('id, nama_orang_tua, alamat, no_whatsapp, nama_anak, jenis_kelamin, tanggal_lahir')
        .order('nama_orang_tua', { ascending: true })

    if (error) {
        return (
            <div className="min-h-screen flex items-center justify-center text-red-500 font-semibold">
                Gagal memuat data.
            </div>
        )
    }

    return (
        <div className="min-h-screen bg-white font-sans text-black flex flex-col">
            {/* Header */}
            <header className="flex justify-between items-center px-8 py-6 border-b border-gray-100">
                <div className="flex items-center gap-4">
                    <Link href="/dashboard" className="group flex items-center gap-2 text-sm font-bold hover:text-gray-600 transition-colors">
                        <div className="p-2 rounded-full border border-black flex items-center justify-center group-hover:bg-black group-hover:text-white transition-all">
                            <ArrowLeft className="h-5 w-5" />
                        </div>
                        <span className="hidden md:block">Kembali</span>
                    </Link>
                    <div className="h-8 w-px bg-gray-200 mx-2 hidden md:block"></div>
                    <div className="flex flex-col justify-center">
                        <h1 className="text-xl font-bold leading-none">Kelola Data</h1>
                        <p className="text-[10px] text-gray-500 tracking-widest uppercase mt-0.5">DATA PENGGUNA TERDAFTAR</p>
                    </div>
                </div>
                <LogoutButton />
            </header>

            <main className="p-8 max-w-5xl mx-auto flex-1 w-full">
                <div className="bg-white rounded-xl border border-black shadow-[8px_8px_0px_0px_rgba(0,0,0,1)] p-8">

                    {/* Card Header */}
                    <div className="flex items-center gap-4 mb-8 pb-6 border-b border-gray-100">
                        <div className="w-14 h-14 rounded-full bg-orange-50 border-2 border-orange-200 flex items-center justify-center text-orange-600">
                            <ClipboardList className="w-7 h-7" />
                        </div>
                        <div>
                            <h2 className="text-2xl font-bold">Daftar Data Pengguna</h2>
                            <p className="text-sm text-gray-500">
                                Review informasi lengkap data akun pengguna yang terdaftar
                            </p>
                        </div>
                        <div className="ml-auto flex items-center gap-3">
                            <CetakInstanModal />
                            <div className="flex items-center gap-2 px-4 py-2 bg-gray-50 border border-gray-200 rounded-lg">
                                <span className="text-2xl font-black">{data?.length ?? 0}</span>
                                <span className="text-xs text-gray-500 font-semibold">Total<br />Pengguna</span>
                            </div>
                        </div>
                    </div>

                    <KelolaDataTable data={data ?? []} />
                </div>
            </main>
        </div>
    )
}
