import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { supabase } from '@/lib/supabase'
import { LogoutButton } from '@/components/logout-button'
import { ArrowLeft, Building2, MapPin, Phone, User, ExternalLink, Map as MapIcon } from 'lucide-react'
import Link from 'next/link'
import PosyanduDetailClient from "./PosyanduDetailClient"

interface Props {
    params: Promise<{ id: string }>
}

export default async function PosyanduDetailPage({ params }: Props) {
    const { id } = await params
    const cookieStore = await cookies()
    const sessionCookie = cookieStore.get('user_session')
    if (!sessionCookie) redirect('/')

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

    const { data: posyandu, error } = await supabase
        .from('detail_posyandu')
        .select(`
            *,
            petugas:petugas_posyandu_lokal(*),
            jadwal:jadwal_posyandu(*)
        `)
        .eq('id', id)
        .order('tanggal', { foreignTable: 'jadwal_posyandu', ascending: false })
        .single()

    if (error || !posyandu) {
        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">Data Tidak Ditemukan</p>
                    <Link href="/user-dashboard/lokasi-posyandu" className="inline-block mt-4 text-xs font-bold underline">Kembali ke Daftar Posyandu</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="/user-dashboard/lokasi-posyandu" 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">{posyandu.nama_posyandu}</h1>
                        <p className="text-[10px] text-gray-500 tracking-widest uppercase mt-0.5">DETAIL LOKASI & ULASAN</p>
                    </div>
                </div>
                <LogoutButton />
            </header>

            <main className="p-8 max-w-6xl mx-auto w-full">
                <PosyanduDetailClient data={posyandu} userId={session.id} />
            </main>
        </div>
    )
}
