"use client";

import { useState } from "react";
import { issueCertificate } from "../actions";
import { Card } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
import { Award, User, BookOpen, Loader2 } from "lucide-react";

export default function IssueCertificateClient({ 
  users, 
  courses, 
  templates 
}: { 
  users: any[], 
  courses: any[], 
  templates: any[] 
}) {
  const [loading, setLoading] = useState(false);

  return (
    <Card className="p-6 bg-rima-surface/50 border border-rima-border relative overflow-hidden">
      {/* Background Glow */}
      <div className="absolute top-0 right-0 w-64 h-64 bg-cyan-500/10 rounded-full blur-[80px] pointer-events-none"></div>

      <form 
        action={async (formData) => {
          setLoading(true);
          try {
            await issueCertificate(formData);
          } catch (error) {
            console.error(error);
            setLoading(false);
          }
        }} 
        className="space-y-6 relative z-10"
      >
        
        {/* User Selection */}
        <div>
          <label className="block text-sm font-bold text-slate-300 mb-2 flex items-center gap-2">
            <User className="w-4 h-4 text-cyan-500" /> Cursant (Student)
          </label>
          <select 
            name="userId" 
            required 
            className="w-full bg-[#0a0c14] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500/50 appearance-none custom-select"
          >
            <option value="">Selectează cursantul...</option>
            {users.map(u => (
              <option key={u.id} value={u.id}>{u.firstName} {u.lastName} ({u.email})</option>
            ))}
          </select>
        </div>

        {/* Course Selection */}
        <div>
          <label className="block text-sm font-bold text-slate-300 mb-2 flex items-center gap-2">
            <BookOpen className="w-4 h-4 text-cyan-500" /> Curs Finalizat
          </label>
          <select 
            name="courseId" 
            required 
            className="w-full bg-[#0a0c14] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500/50 appearance-none custom-select"
          >
            <option value="">Selectează cursul absolvit...</option>
            {courses.map(c => (
              <option key={c.id} value={c.id}>{c.title}</option>
            ))}
          </select>
        </div>

        {/* Template Selection */}
        <div>
          <label className="block text-sm font-bold text-slate-300 mb-2 flex items-center gap-2">
            <Award className="w-4 h-4 text-cyan-500" /> Șablon Certificat
          </label>
          <select 
            name="templateId" 
            className="w-full bg-[#0a0c14] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500/50 appearance-none custom-select"
          >
            <option value="">Folosește șablonul implicit (Default)</option>
            {templates.map(t => (
              <option key={t.id} value={t.id}>{t.name} {t.isDefault ? "(Implicit)" : ""}</option>
            ))}
          </select>
        </div>

        <div className="pt-4 border-t border-white/10 flex justify-end">
          <Button 
            type="submit" 
            disabled={loading}
            className="w-full sm:w-auto px-8 py-3 bg-gradient-to-r from-cyan-500 to-blue-500 text-white font-bold rounded-xl hover:scale-[1.02] transition-all shadow-[0_0_20px_rgba(6,182,212,0.3)] disabled:opacity-50 disabled:hover:scale-100 flex items-center justify-center gap-2"
          >
            {loading ? <Loader2 className="w-5 h-5 animate-spin" /> : <Award className="w-5 h-5" />}
            Emite Certificatul Oficial
          </Button>
        </div>

      </form>
    </Card>
  );
}
