import { verifySession } from "@/lib/auth";
import { pgGetChartOfAccounts, pgGetGeneralBanks } from "@/lib/postgres/data-access";
import { getCurrentLocale, getTranslations } from "@/lib/i18n";
import { GeneralBanksManager } from "@/components/admin/general-banks-manager";
import type { ChartOfAccount, GeneralBank } from "@/lib/types";

export default async function ManageGeneralBanksPage() {
  const session = await verifySession();
  const user = session.user;
  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tGlobal = t.Global ?? {};
  const tLists = t.ItemLists ?? {};

  if (!user || user.role !== 'admin') {
    return (
      <div className="text-center">
        <h1 className="text-2xl font-bold">{tGlobal.accessDenied ?? 'Access Denied'}</h1>
        <p>{tGlobal.noPermission ?? 'You do not have permission to view this page.'}</p>
      </div>
    );
  }

  const banks: GeneralBank[] = await pgGetGeneralBanks() as GeneralBank[];
  const chartOfAccounts: ChartOfAccount[] = (await pgGetChartOfAccounts({ page: 1, pageSize: 5000 })).items as ChartOfAccount[];

  return (
    <GeneralBanksManager
      initialBanks={banks}
      chartOfAccounts={chartOfAccounts}
      t={tLists}
      tGlobal={tGlobal}
    />
  );
}
