import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { createFischereiApi } from '@kit/fischerei/api';
import { FischereiTabNavigation } from '@kit/fischerei/components';

import { CmsPageShell } from '~/components/cms-page-shell';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
import { AccountNotFound } from '~/components/account-not-found';

interface Props {
  params: Promise<{ account: string }>;
}

export default async function PermitsPage({ params }: Props) {
  const { account } = await params;
  const client = getSupabaseServerClient();

  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();

  if (!acct) return <AccountNotFound />;

  const api = createFischereiApi(client);
  const permits = await api.listPermits(acct.id);

  return (
    <CmsPageShell account={account} title="Fischerei - Erlaubnisscheine">
      <FischereiTabNavigation account={account} activeTab="permits" />
      <div className="flex w-full flex-col gap-6">
        <div>
          <h1 className="text-2xl font-bold">Erlaubnisscheine</h1>
          <p className="text-muted-foreground">
            Erlaubnisscheine und Gewässerkarten verwalten
          </p>
        </div>
        <Card>
          <CardHeader>
            <CardTitle>Erlaubnisscheine ({permits.length})</CardTitle>
          </CardHeader>
          <CardContent>
            {permits.length === 0 ? (
              <div className="flex flex-col items-center justify-center rounded-lg border border-dashed p-12 text-center">
                <h3 className="text-lg font-semibold">
                  Keine Erlaubnisscheine vorhanden
                </h3>
                <p className="mt-1 max-w-sm text-sm text-muted-foreground">
                  Erstellen Sie Ihren ersten Erlaubnisschein.
                </p>
              </div>
            ) : (
              <div className="rounded-md border">
                <table className="w-full text-sm">
                  <thead>
                    <tr className="border-b bg-muted/50">
                      <th className="p-3 text-left font-medium">Bezeichnung</th>
                      <th className="p-3 text-left font-medium">Kurzcode</th>
                      <th className="p-3 text-left font-medium">Hauptgewässer</th>
                      <th className="p-3 text-right font-medium">Gesamtmenge</th>
                      <th className="p-3 text-center font-medium">Zum Verkauf</th>
                    </tr>
                  </thead>
                  <tbody>
                    {permits.map((permit: Record<string, unknown>) => {
                      const waters = permit.waters as Record<string, unknown> | null;

                      return (
                        <tr key={String(permit.id)} className="border-b hover:bg-muted/30">
                          <td className="p-3 font-medium">{String(permit.name)}</td>
                          <td className="p-3 text-muted-foreground">
                            {String(permit.short_code ?? '—')}
                          </td>
                          <td className="p-3">
                            {waters ? String(waters.name) : '—'}
                          </td>
                          <td className="p-3 text-right">
                            {permit.total_quantity != null
                              ? String(permit.total_quantity)
                              : '—'}
                          </td>
                          <td className="p-3 text-center">
                            {permit.is_for_sale ? '✓' : '—'}
                          </td>
                        </tr>
                      );
                    })}
                  </tbody>
                </table>
              </div>
            )}
          </CardContent>
        </Card>
      </div>
    </CmsPageShell>
  );
}
