export const dynamic = "force-dynamic";
import { db } from "@/lib/db";
import { Award, Upload, Settings, FileText, CheckCircle2, Clock } from "lucide-react";
import Link from "next/link";
import { Badge } from "@/components/ui/Badge";
import GeneratorButton from "./GeneratorButton";

export default async function AdminCertificatesPage() {
  // @ts-ignore
  const templates = await db.certificateTemplate.findMany({
    orderBy: { createdAt: "desc" }
  });

  // @ts-ignore
  const certificates = await db.certificate.findMany({
    include: {
      user: true,
      course: true,
      template: true,
    },
    orderBy: { createdAt: "desc" }
  });

  return (
    <div className="space-y-8 animate-fade-in">
      {/* Header */}
      <div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <div>
          <h1 className="text-3xl font-bold text-white mb-1 tracking-tight font-heading">Certificates Engine</h1>
          <p className="text-rima-muted text-sm">Gestionează șabloanele și vizualizează diplomele emise.</p>
        </div>
        <GeneratorButton />
      </div>

      {/* Templates Section */}
      <div>
        <h2 className="text-xl font-bold text-white mb-4 font-heading">Șabloane Certificate</h2>
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
          {templates.length === 0 ? (
            <div className="md:col-span-3 bg-rima-surface/50 border border-rima-border rounded-2xl p-8 text-center text-rima-muted flex flex-col items-center justify-center min-h-[200px]">
              <Settings className="w-10 h-10 mb-4 opacity-50" />
              <p>Niciun șablon adăugat. Sistemul va folosi un design default (fallback) până vei încărca unul.</p>
            </div>
          ) : templates.map((t: any) => (
            <div key={t.id} className="bg-rima-surface/50 border border-rima-border rounded-2xl p-6 relative group overflow-hidden">
               <div className="absolute top-0 right-0 p-4">
                 {t.isDefault && <Badge variant="success">Default</Badge>}
               </div>
               <Award className="w-10 h-10 text-rima-primary mb-4" />
               <h3 className="font-bold text-white mb-1">{t.name}</h3>
               <p className="text-xs text-rima-muted line-clamp-2">{t.description}</p>
            </div>
          ))}
        </div>
      </div>

      {/* Issued Certificates Section */}
      <div>
         <h2 className="text-xl font-bold text-white mb-4 font-heading">Diplome Emise</h2>
         <div className="bg-rima-surface/50 border border-rima-border rounded-2xl overflow-hidden shadow-xl">
          <div className="overflow-x-auto custom-scrollbar">
            <table className="w-full text-left border-collapse">
              <thead>
                <tr className="bg-rima-background border-b border-rima-border text-xs uppercase tracking-widest text-rima-muted">
                  <th className="px-6 py-4 font-bold">ID Certificat</th>
                  <th className="px-6 py-4 font-bold">Student</th>
                  <th className="px-6 py-4 font-bold">Curs</th>
                  <th className="px-6 py-4 font-bold">Status</th>
                  <th className="px-6 py-4 font-bold text-right">Acțiuni</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-rima-border text-sm">
                {certificates.length === 0 ? (
                  <tr>
                    <td colSpan={5} className="px-6 py-12 text-center text-rima-muted">
                      Nu au fost emise certificate încă.
                    </td>
                  </tr>
                ) : certificates.map((cert: any) => (
                  <tr key={cert.id} className="hover:bg-white/[0.02] transition-colors group">
                    <td className="px-6 py-4">
                      <div className="font-bold text-white mb-1">
                        {cert.certificateId}
                      </div>
                      <div className="text-xs text-rima-muted">
                        Emis: {cert.issueDate ? new Intl.DateTimeFormat('ro-RO').format(new Date(cert.issueDate)) : "N/A"}
                      </div>
                    </td>
                    <td className="px-6 py-4">
                      <div className="font-bold text-white">
                        {cert.user.firstName} {cert.user.lastName}
                      </div>
                      <div className="text-xs text-rima-primary">
                        {cert.user.email}
                      </div>
                    </td>
                    <td className="px-6 py-4">
                      <div className="font-bold text-white line-clamp-1 max-w-[250px]">
                        {cert.course.title}
                      </div>
                    </td>
                    <td className="px-6 py-4">
                       <Badge variant={cert.status === 'ISSUED' ? 'success' : 'warning'}>
                          {cert.status}
                       </Badge>
                    </td>
                    <td className="px-6 py-4">
                      <div className="flex items-center justify-end gap-3 opacity-0 group-hover:opacity-100 transition-opacity">
                        <Link href={`/certificates/${cert.id}`} target="_blank" className="flex items-center gap-2 px-3 py-1.5 bg-rima-background hover:bg-white/5 border border-rima-border rounded-lg text-xs font-medium text-white transition-colors">
                          <FileText className="w-3.5 h-3.5 text-rima-primary" /> Vezi PDF
                        </Link>
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </div>

    </div>
  );
}
