import { verifySession } from "@/lib/auth";
import { pgGetBanks, pgGetChartOfAccounts, pgGetCurrencies } from "@/lib/postgres/data-access";
import { getCurrentLocale, getTranslations } from "@/lib/i18n";
import { BanksManager } from "@/components/admin/banks-manager";
import type { Bank, ChartOfAccount, Currency } from "@/lib/types";

export default async function ManageBanksPage() {
  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: Bank[] = (await pgGetBanks({ page: 1, pageSize: 5000 })).items as Bank[];
  const currencies: Currency[] = (await pgGetCurrencies({ page: 1, pageSize: 5000 })).items as Currency[];
  const chartOfAccounts: ChartOfAccount[] = (await pgGetChartOfAccounts({ page: 1, pageSize: 5000 })).items as ChartOfAccount[];

  return (
    <BanksManager
      initialBanks={banks}
      currencies={currencies}
      chartOfAccounts={chartOfAccounts}
      t={tLists}
      tGlobal={tGlobal}
    />
  );
}
