import { ServerDataLoader } from '@makerkit/data-loader-supabase-nextjs';
import { Trans } from 'react-i18next';

import { AdminAccountsTable } from '@kit/admin/components/admin-accounts-table';
import { AdminCreateCompanyDialog } from '@kit/admin/components/admin-create-company-dialog';
import { AdminCreateUserDialog } from '@kit/admin/components/admin-create-user-dialog';
import { AdminGuard } from '@kit/admin/components/admin-guard';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
import { Button } from '@kit/ui/button';
import { PageBody, PageHeader } from '@kit/ui/page';

interface SearchParams {
  page?: string;
  account_type?: 'all' | 'team' | 'personal';
  query?: string;
}

interface AdminAccountsPageProps {
  searchParams: Promise<SearchParams>;
}

export const metadata = {
  title: `Accounts`,
};

async function AccountsPage(props: AdminAccountsPageProps) {
  const client = getSupabaseServerClient().schema('medreport');
  const searchParams = await props.searchParams;
  const page = searchParams.page ? parseInt(searchParams.page) : 1;

  return (
    <>
      <PageHeader description={<AppBreadcrumbs />}>
        <div className="flex justify-end gap-2">
          <AdminCreateUserDialog>
            <Button data-test="admin-create-user-button">
              Create Personal Account
            </Button>
          </AdminCreateUserDialog>
          <AdminCreateCompanyDialog>
            <Button>
              <Trans i18nKey="account:createCompanyAccount" />
            </Button>
          </AdminCreateCompanyDialog>
        </div>
      </PageHeader>

      <PageBody>
        <ServerDataLoader
          table="accounts"
          client={client}
          page={page}
          where={(queryBuilder) => {
            const { account_type: type, query } = searchParams;

            if (type && type !== 'all') {
              queryBuilder.eq('is_personal_account', type === 'personal');
            }

            if (query) {
              queryBuilder.or(
                `name.ilike.%${query}%,email.ilike.%${query}%,personal_code.ilike.%${query}%`,
              );
            }

            return queryBuilder;
          }}
        >
          {({ data, page, pageSize, pageCount }) => {
            return (
              <AdminAccountsTable
                page={page}
                pageSize={pageSize}
                pageCount={pageCount}
                data={data}
                filters={{
                  type: searchParams.account_type ?? 'all',
                  query: searchParams.query ?? '',
                }}
              />
            );
          }}
        </ServerDataLoader>
      </PageBody>
    </>
  );
}

export default AdminGuard(AccountsPage);
