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

export default async function KelolaAkunPetugasPage() {
    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: petugas, error } = await supabase
        .from('petugas_posyandu')
        .select('*')
        .eq('id', session.id)
        .single()

    if (error || !petugas) {
        return <div>Error loading profile.</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/manajemen-akun" 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 Akun Petugas</h1>
                        <p className="text-[10px] text-gray-500 tracking-widest uppercase mt-0.5">EDIT PROFIL ANDA</p>
                    </div>
                </div>
                <LogoutButton />
            </header>

            <main className="p-8 max-w-2xl 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">

                    <div className="flex items-center gap-4 mb-8 pb-6 border-b border-gray-100">
                        <div className="w-16 h-16 rounded-full bg-green-50 border-2 border-green-200 flex items-center justify-center text-green-600">
                            <UserCog className="w-8 h-8" />
                        </div>
                        <div>
                            <h2 className="text-2xl font-bold">Informasi Akun</h2>
                            <p className="text-gray-500 text-sm">Perbarui informasi akun petugas anda di sini.</p>
                        </div>
                    </div>

                    <EditPetugasForm petugas={petugas} />

                </div>
            </main>
        </div>
    )
}
