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

export default async function ProductsPage() {
    const supabase = createClient()
    const { data: products } = await supabase
        .from("products")
        .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">
                <h2 className="text-3xl font-bold tracking-tight">Ürünler</h2>
                <div className="flex items-center space-x-2">
                    <Link href="/dashboard/products/new">
                        <Button>
                            <Plus className="mr-2 h-4 w-4" /> Yeni Ekle
                        </Button>
                    </Link>
                </div>
            </div>

            <div className="border rounded-md overflow-x-auto">
                <Table>
                    <TableHeader>
                        <TableRow>
                            <TableHead>Ad</TableHead>
                            <TableHead>Kategori</TableHead>
                            <TableHead>Fiyat</TableHead>
                            <TableHead className="text-right">İşlemler</TableHead>
                        </TableRow>
                    </TableHeader>
                    <TableBody>
                        {products?.length === 0 ? (
                            <TableRow>
                                <TableCell colSpan={4} className="h-24 text-center">
                                    Henüz ürün eklenmemiş.
                                </TableCell>
                            </TableRow>
                        ) : (
                            products?.map((product) => (
                                <TableRow key={product.id}>
                                    <TableCell className="font-medium">{product.name}</TableCell>
                                    <TableCell>
                                        <Badge variant="secondary" className="capitalize">
                                            {product.category}
                                        </Badge>
                                    </TableCell>
                                    <TableCell>₺{product.price}</TableCell>
                                    <TableCell className="text-right">
                                        <Link href={`/dashboard/products/${product.id}`}>
                                            <Button variant="ghost" size="sm">Düzenle</Button>
                                        </Link>
                                    </TableCell>
                                </TableRow>
                            ))
                        )}
                    </TableBody>
                </Table>
            </div>
        </div>
    )
}
