import Link from "next/link";
import { db } from "@/lib/db";
import { AdminPageHeader } from "@/components/admin/page-header";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Icon } from "@/components/site/icon";
import { Plus, Pencil, GripVertical, Eye, EyeOff } from "lucide-react";

export const dynamic = "force-dynamic";

export default async function AdminServicesPage() {
  const services = await db.service.findMany({ orderBy: { order: "asc" } });

  return (
    <>
      <AdminPageHeader title="Services" description={`${services.length} services configured`}>
        <Button asChild className="brand-gradient text-white">
          <Link href="/admin/services/new"><Plus className="size-4 mr-1.5" /> Add Service</Link>
        </Button>
      </AdminPageHeader>

      <Card className="rounded-2xl overflow-hidden">
        <div className="divide-y divide-border">
          {services.map((s) => (
            <div key={s.id} className="flex items-center gap-4 p-4 hover:bg-accent/40 transition-colors">
              <GripVertical className="size-4 text-muted-foreground/40 shrink-0" />
              <div className="grid place-items-center size-10 rounded-lg bg-[var(--brand-royal)]/10 text-[var(--brand-royal)] shrink-0">
                <Icon name={s.icon} className="size-5" />
              </div>
              <div className="min-w-0 flex-1">
                <div className="flex items-center gap-2">
                  <span className="font-semibold truncate">{s.title}</span>
                  {!s.isEnabled && <Badge variant="secondary" className="text-xs">Disabled</Badge>}
                </div>
                <div className="text-xs text-muted-foreground truncate">/{s.slug}</div>
              </div>
              <div className="hidden sm:block text-xs text-muted-foreground">Order: {s.order}</div>
              <Button asChild variant="ghost" size="sm">
                <Link href={`/services/${s.slug}`} target="_blank">{s.isEnabled ? <Eye className="size-4" /> : <EyeOff className="size-4" />}</Link>
              </Button>
              <Button asChild variant="outline" size="sm">
                <Link href={`/admin/services/edit/${s.id}`}><Pencil className="size-3.5 mr-1" /> Edit</Link>
              </Button>
            </div>
          ))}
          {services.length === 0 && (
            <div className="p-12 text-center text-muted-foreground">No services yet. Click "Add Service" to create one.</div>
          )}
        </div>
      </Card>
    </>
  );
}
