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

export default async function ManajemenPosyanduPage() {
    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')

    // Fetch detail_posyandu with its petugas
    const { data: posyandu, error } = await supabase
        .from('detail_posyandu')
        .select(`
            *,
            petugas:petugas_posyandu_lokal(*)
        `)
        .order('nama_posyandu', { ascending: true })

    if (error) {
        return (
            <div className="min-h-screen flex items-center justify-center text-red-500 font-semibold bg-white p-8">
                <div className="bg-red-50 p-6 rounded-2xl border-2 border-red-200 text-center max-w-md">
                    <p className="font-black text-xl mb-2">Gagal Memuat Data</p>
                    <p className="text-sm opacity-80">{error.message}</p>
                    <Link href="/dashboard" className="inline-block mt-4 text-xs font-bold underline">Kembali ke Dashboard</Link>
                </div>
            </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 text-purple-600">Manajemen Posyandu</h1>
                        <p className="text-[10px] text-gray-500 tracking-widest uppercase mt-0.5">ADMINISTRASI WILAYAH</p>
                    </div>
                </div>
                <LogoutButton />
            </header>

            <main className="p-8 max-w-6xl mx-auto flex-1 w-full flex flex-col gap-8">

                {/* Hero Section */}
                <div className="flex flex-col md:flex-row items-center gap-8 bg-purple-50 p-8 rounded-3xl border-2 border-purple-100 relative overflow-hidden">
                    <div className="flex-1 z-10">
                        <h2 className="text-3xl font-black mb-2 text-purple-900">Kelola Titik Layanan</h2>
                        <p className="text-purple-600/80 max-w-lg leading-relaxed font-semibold">
                            Tambah, edit, atau hapus lokasi posyandu yang aktif di wilayah ini. Pastikan titik koordinat Google Maps sudah sesuai untuk kemudahan navigasi warga.
                        </p>
                    </div>
                    <div className="w-20 h-20 bg-purple-600 rounded-2xl flex items-center justify-center text-white shadow-[8px_8px_0px_0px_rgba(0,0,0,1)] rotate-3 z-10">
                        <Building2 className="w-10 h-10" />
                    </div>
                    {/* Subtle decoration */}
                    <div className="absolute -right-10 -bottom-10 w-48 h-48 bg-purple-200/50 rounded-full blur-3xl"></div>
                </div>

                {/* Table Component */}
                <ManajemenPosyanduTable data={posyandu ?? []} />

            </main>
        </div>
    )
}
