import { redirect } from 'next/navigation';
import { verifySession } from '@/lib/auth';

import { pgGetItemUnits, pgGetMaterials, pgGetWarehouses } from '@/lib/postgres/data-access';
import { getTranslations, getCurrentLocale } from '@/lib/i18n';
import ItemUnitsManager from '@/components/admin/item-units-manager';
import type { ItemUnit, ProductionItem, Warehouse } from '@/lib/types';

export default async function ItemUnitsPage() {
  const session = await verifySession();
  const user = session.user;

  if (!user || user.role !== 'admin') {
    redirect('/login');
  }

  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tItemUnits = t.ItemUnits ?? {};
  const tGlobal = t.Global ?? {};
  
  const materials: ProductionItem[] = (await pgGetMaterials({ page: 1, pageSize: 5000 })).items as ProductionItem[];
  const itemUnits: ItemUnit[] = (await pgGetItemUnits({ page: 1, pageSize: 5000 })).items as ItemUnit[];
  const warehouses: Warehouse[] = (await pgGetWarehouses({ page: 1, pageSize: 5000 })).items as Warehouse[];

  return (
    <div className="container mx-auto py-6 px-4 max-w-7xl">
      <ItemUnitsManager
        materials={materials}
        itemUnits={itemUnits}
        warehouses={warehouses}
        t={tItemUnits}
        tGlobal={tGlobal}
      />
    </div>
  );
}
