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

export default async function UsersPage() {
    const supabase = await createClient()

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

    // We might also want to get email from auth.users, but we can't join that easily with RLS/client.
    // However, usually profiles table should have email or we just show name.
    // In the schema, profiles has: id, role, full_name. No email.
    // This is a limitation. We can only show what's in profiles.
    // Unless we have a way to query auth.users (requires admin).

    return (
        <div className="space-y-6">
            <div className="flex items-center justify-between">
                <div>
                    <h2 className="text-3xl font-bold tracking-tight">Kullanıcı Yönetimi</h2>
                    <p className="text-muted-foreground">Sisteme erişimi olan kullanıcıları yönetin.</p>
                </div>
                <Link href="/dashboard/settings/users/new">
                    <Button>
                        <Plus className="mr-2 h-4 w-4" /> Yeni Kullanıcı
                    </Button>
                </Link>
            </div>

            <div className="rounded-md border hidden md:block">
                <Table>
                    <TableHeader>
                        <TableRow>
                            <TableHead>İsim Soyisim</TableHead>
                            <TableHead>Rol</TableHead>
                            <TableHead>Oluşturulma Tarihi</TableHead>
                            <TableHead className="text-right">İşlemler</TableHead>
                        </TableRow>
                    </TableHeader>
                    <TableBody>
                        {profiles?.length === 0 ? (
                            <TableRow>
                                <TableCell colSpan={4} className="text-center h-24 text-muted-foreground">
                                    Kullanıcı bulunamadı.
                                </TableCell>
                            </TableRow>
                        ) : (
                            profiles?.map((profile) => (
                                <TableRow key={profile.id}>
                                    <TableCell className="font-medium">
                                        <div className="flex items-center gap-2">
                                            <div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center text-primary font-bold text-xs">
                                                {profile.full_name?.substring(0, 2).toUpperCase() || 'US'}
                                            </div>
                                            {profile.full_name || 'İsimsiz Kullanıcı'}
                                        </div>
                                    </TableCell>
                                    <TableCell>
                                        <Badge variant={profile.role === 'admin' ? 'default' : 'secondary'}>
                                            {profile.role === 'admin' ? 'Yönetici' : 'Personel'}
                                        </Badge>
                                    </TableCell>
                                    <TableCell>
                                        {new Date(profile.created_at).toLocaleDateString('tr-TR')}
                                    </TableCell>
                                    <TableCell className="text-right">
                                        <Link href={`/dashboard/settings/users/${profile.id}`}>
                                            <Button variant="ghost" size="sm">Düzenle</Button>
                                        </Link>
                                    </TableCell>
                                </TableRow>
                            ))
                        )}
                    </TableBody>
                </Table>
            </div>

            {/* Mobile View */}
            <div className="grid grid-cols-1 gap-4 md:hidden">
                {profiles?.map((profile) => (
                    <Card key={profile.id}>
                        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                            <CardTitle className="text-sm font-medium">
                                {profile.full_name || 'İsimsiz'}
                            </CardTitle>
                            <Badge variant={profile.role === 'admin' ? 'default' : 'secondary'}>
                                {profile.role === 'admin' ? 'Yönetici' : 'Personel'}
                            </Badge>
                        </CardHeader>
                        <CardContent>
                            <div className="text-xs text-muted-foreground mt-2">
                                Kayıt: {new Date(profile.created_at).toLocaleDateString('tr-TR')}
                            </div>
                        </CardContent>
                    </Card>
                ))}
            </div>
        </div>
    )
}
