import { createClient } from "@/lib/supabase-server"
import { Button } from "@/components/ui/button"
import Link from "next/link"
import { Plus } from "lucide-react"
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/components/ui/table"
import { Badge } from "@/components/ui/badge"

export default async function UsersPage() {
    const supabase = createClient()
    const { data: { user } } = await supabase.auth.getUser()

    // Protected Route Check (Simple)
    const { data: currentUserProfile } = await supabase
        .from('profiles')
        .select('role')
        .eq('id', user?.id)
        .single()

    // Only verify if we have profiles, if not (first run), maybe allow? 
    // But for safety, blocking non-admins.
    if (currentUserProfile?.role !== 'admin') {
        return (
            <div className="flex-1 p-8 pt-6">
                <h2 className="text-3xl font-bold tracking-tight text-red-600">Yetkisiz Erişim</h2>
                <p>Bu sayfayı görüntüleme yetkiniz yok.</p>
            </div>
        )
    }

    const { data: profiles } = await supabase
        .from("profiles")
        .select("*")
        .order("created_at", { ascending: false })

    return (
        <div className="flex-1 space-y-4 p-8 pt-6">
            <div className="flex items-center justify-between space-y-2">
                <h2 className="text-3xl font-bold tracking-tight">Kullanıcı Yönetimi</h2>
                <div className="flex items-center space-x-2">
                    <Link href="/dashboard/users/new">
                        <Button>
                            <Plus className="mr-2 h-4 w-4" /> Yeni Kullanıcı
                        </Button>
                    </Link>
                </div>
            </div>

            <div className="rounded-md border">
                <Table>
                    <TableHeader>
                        <TableRow>
                            <TableHead>Ad Soyad</TableHead>
                            <TableHead>Rol</TableHead>
                            <TableHead>Kayıt Tarihi</TableHead>
                            <TableHead className="text-right">İşlemler</TableHead>
                        </TableRow>
                    </TableHeader>
                    <TableBody>
                        {profiles?.map((profile) => (
                            <TableRow key={profile.id}>
                                <TableCell className="font-medium">{profile.full_name}</TableCell>
                                <TableCell>
                                    <Badge variant={profile.role === 'admin' ? 'default' : 'secondary'}>
                                        {profile.role === 'admin' ? 'Yönetici' : 'Kullanıcı'}
                                    </Badge>
                                </TableCell>
                                <TableCell>{new Date(profile.created_at).toLocaleDateString('tr-TR')}</TableCell>
                                <TableCell className="text-right">
                                    <Link href={`/dashboard/users/${profile.id}`}>
                                        <Button variant="ghost" size="sm">Düzenle</Button>
                                    </Link>
                                </TableCell>
                            </TableRow>
                        ))}
                    </TableBody>
                </Table>
            </div>
        </div>
    )
}
