import { FileText } from 'lucide-react';

import { Head, Link, router } from '@inertiajs/react';

import { Button } from '@/Components/ui/button';
import { EmptyState } from '@/Components/ui/empty-state';
import DashboardLayout from '@/Layouts/DashboardLayout';

interface Template {
  id: number;
  name: string;
  description: string | null;
  sections: string[];
  filters: Record<string, unknown>;
  is_system: boolean;
  created_at: string;
}

interface IndexProps {
  templates: Template[];
  filters: {
    type?: string;
    search?: string;
    sort?: string;
  };
  counts: {
    total: number;
    system: number;
    user: number;
  };
  pagination: {
    current_page: number;
    last_page: number;
    per_page: number;
    total: number;
  };
}

export default function ReportTemplatesIndex({
  templates,
}: IndexProps) {
  return (
    <DashboardLayout>
      <Head title="Report Templates" />

      <div className="space-y-6">
        <div className="flex items-center justify-between">
          <div>
            <h1 className="text-3xl font-bold text-foreground">Report Templates</h1>
            <p className="text-sm text-muted-foreground">Manage your report templates</p>
          </div>
          <Button
            onClick={() => router.visit(route('report-templates.index'), { data: { create: 1 } })}
          >
            Create Template
          </Button>
        </div>

        <div className="space-y-4">
          {templates.length > 0 ? (
            <div className="divide-y rounded-lg border border-border bg-card">
              {templates.map((template) => (
                <Link
                  key={template.id}
                  href={route('report-templates.show', template.id)}
                  className="block p-4 transition hover:bg-muted"
                >
                  <div className="flex items-start justify-between">
                    <div className="flex-1">
                      <h3 className="font-semibold text-foreground">{template.name}</h3>
                      {template.description && (
                        <p className="mt-1 text-sm text-muted-foreground">{template.description}</p>
                      )}
                      <div className="mt-2 flex gap-2">
                        {template.is_system && (
                          <span className="inline-block rounded bg-info/10 px-2 py-1 text-xs font-medium text-info">
                            System
                          </span>
                        )}
                        <span className="inline-block rounded bg-muted px-2 py-1 text-xs font-medium text-muted-foreground">
                          {template.sections.length} sections
                        </span>
                      </div>
                    </div>
                  </div>
                </Link>
              ))}
            </div>
          ) : (
            <EmptyState
              icon={FileText}
              title="No templates found"
              description="Create your first report template to reuse across sites."
              action={
                <Button
                  onClick={() =>
                    router.visit(route('report-templates.index'), { data: { create: 1 } })
                  }
                >
                  Create Template
                </Button>
              }
            />
          )}
        </div>
      </div>
    </DashboardLayout>
  );
}
