import Link from "next/link";
import { Plus, Search, Edit, Trash2, Eye, EyeOff } from "lucide-react";
export const dynamic = "force-dynamic";
import { db } from "@/lib/db";
import { Card } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
import { Badge } from "@/components/ui/Badge";
import { Input } from "@/components/ui/Input";

export default async function AdminCoursesPage() {
  const courses = await db.course.findMany({
    orderBy: { createdAt: "desc" },
    include: {
      language: true,
      level: true,
      modules: {
        include: { lessons: true }
      }
    }
  });

  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-rima-text mb-1 tracking-tight">Courses & Modules</h1>
          <p className="text-rima-muted">Gestionează programa academiei și materialele educaționale.</p>
        </div>
        <Link href="/admin/courses/new">
          <Button>
            <Plus className="w-5 h-5 mr-2" />
            Crează Curs Nou
          </Button>
        </Link>
      </div>

      {/* Courses List */}
      <Card className="p-0 overflow-hidden">
        <div className="p-6 border-b border-rima-border flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
          <h2 className="text-lg font-bold text-rima-text tracking-tight">Toate Cursurile</h2>
          <div className="w-full sm:w-64">
            <div className="relative">
              <Search className="w-4 h-4 text-rima-muted absolute left-3 top-1/2 -translate-y-1/2" />
              <input 
                type="text" 
                placeholder="Caută cursuri..." 
                className="w-full bg-rima-surface/50 border border-rima-border rounded-xl pl-10 pr-4 py-2 text-sm text-rima-text placeholder-rima-muted focus:outline-none focus:border-rima-primary transition-colors"
              />
            </div>
          </div>
        </div>

        <div className="overflow-x-auto">
          <table className="w-full text-left border-collapse">
            <thead>
              <tr className="bg-rima-surface/50 border-b border-rima-border">
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest">Nume Curs</th>
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest">Limbă & Nivel</th>
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest">Preț</th>
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest">Conținut</th>
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest">Status</th>
                <th className="px-6 py-4 text-xs font-bold text-rima-muted uppercase tracking-widest text-right">Acțiuni</th>
              </tr>
            </thead>
            <tbody>
              {courses.length === 0 ? (
                <tr>
                  <td colSpan={6} className="px-6 py-12 text-center text-rima-muted">
                    Nu a fost găsit niciun curs. Apasă pe "Crează Curs Nou" pentru a începe.
                  </td>
                </tr>
              ) : (
                courses.map((course) => {
                  const totalLessons = course.modules.reduce((acc, mod) => acc + mod.lessons.length, 0);
                  return (
                    <tr key={course.id} className="group hover:bg-rima-surface/30 transition-colors border-b border-rima-border last:border-0">
                      <td className="px-6 py-4">
                        <div className="font-bold text-rima-text mb-1 group-hover:text-rima-primary transition-colors">{course.title}</div>
                        <div className="text-xs text-rima-muted line-clamp-1">{course.description || "Nicio descriere"}</div>
                      </td>
                      <td className="px-6 py-4">
                        <div className="flex gap-2 items-center">
                          <span className="text-xs font-mono bg-rima-surface border border-rima-border px-2 py-1 rounded">
                            {course.language?.code.toUpperCase() || 'N/A'}
                          </span>
                          <span className="text-xs font-bold text-rima-muted">
                            {course.level?.name || 'Fără Nivel'}
                          </span>
                        </div>
                      </td>
                      <td className="px-6 py-4 text-rima-text/80 font-medium">
                        {course.price ? `${course.price} RON` : "Gratuit"}
                      </td>
                      <td className="px-6 py-4 text-rima-muted">
                        <div className="text-xs">{course.modules.length} Module</div>
                        <div className="text-xs">{totalLessons} Lecții</div>
                      </td>
                      <td className="px-6 py-4">
                        {course.isPublished ? (
                          <Badge variant="success" className="gap-1.5 px-3 py-1.5">
                            <Eye className="w-3 h-3" /> Publicat
                          </Badge>
                        ) : (
                          <Badge variant="warning" className="gap-1.5 px-3 py-1.5">
                            <EyeOff className="w-3 h-3" /> Ciornă
                          </Badge>
                        )}
                      </td>
                      <td className="px-6 py-4 text-right">
                        <div className="flex items-center justify-end gap-2">
                          <Link href={`/admin/courses/${course.id}`}>
                            <Button variant="secondary" size="sm" className="p-2 h-auto text-rima-muted hover:text-rima-text" title="Editează Curs">
                              <Edit className="w-4 h-4" />
                            </Button>
                          </Link>
                          <Button variant="ghost" size="sm" className="p-2 h-auto text-rima-danger hover:bg-rima-danger/10 hover:text-rima-danger" title="Șterge Curs">
                            <Trash2 className="w-4 h-4" />
                          </Button>
                        </div>
                      </td>
                    </tr>
                  );
                })
              )}
            </tbody>
          </table>
        </div>
      </Card>
    </div>
  );
}
