import { Users } from 'lucide-react';
import { getTranslations } from 'next-intl/server';

import { createMemberServices } from '@kit/member-management/services';
import { getSupabaseServerClient } from '@kit/supabase/server-client';

import { AccountNotFound } from '~/components/account-not-found';
import { CmsPageShell } from '~/components/cms-page-shell';
import { EmptyState } from '~/components/empty-state';

import { CreateDepartmentDialog } from './create-department-dialog';
import { DeleteDepartmentButton } from './delete-department-button';

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

export default async function DepartmentsPage({ params }: Props) {
  const { account } = await params;
  const client = getSupabaseServerClient();
  const t = await getTranslations('members');
  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();
  if (!acct) return <AccountNotFound />;

  const { organization } = createMemberServices(client);
  const departments = await organization.listDepartments(acct.id);

  return (
    <CmsPageShell
      account={account}
      title={t('departments.title')}
      description={t('departments.subtitle')}
    >
      <div className="space-y-4">
        <div className="flex items-center justify-end">
          <CreateDepartmentDialog accountId={acct.id} />
        </div>

        {departments.length === 0 ? (
          <EmptyState
            icon={<Users className="h-8 w-8" />}
            title={t('departments.noDepartments')}
            description={t('departments.createFirst')}
          />
        ) : (
          <div className="overflow-x-auto rounded-md border">
            <table className="w-full min-w-[640px] text-sm">
              <thead>
                <tr className="bg-muted/50 border-b">
                  <th scope="col" className="p-3 text-left font-medium">
                    {t('departments.name')}
                  </th>
                  <th scope="col" className="p-3 text-left font-medium">
                    {t('departments.description')}
                  </th>
                  <th scope="col" className="p-3 text-left font-medium">
                    {t('departments.actions')}
                  </th>
                </tr>
              </thead>
              <tbody>
                {departments.map((dept: Record<string, unknown>) => (
                  <tr
                    key={String(dept.id)}
                    className="hover:bg-muted/30 border-b"
                  >
                    <td className="p-3 font-medium">{String(dept.name)}</td>
                    <td className="text-muted-foreground p-3">
                      {String(dept.description ?? '—')}
                    </td>
                    <td className="p-3">
                      <div className="flex items-center gap-2">
                        <DeleteDepartmentButton
                          departmentId={String(dept.id)}
                          departmentName={String(dept.name)}
                          accountId={acct.id}
                        />
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </CmsPageShell>
  );
}
