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

import { pgGetMaterials } from '@/lib/postgres/data-access';
import { handleListSalesInvoices } from '@/lib/actions/sales-invoice.actions';
import SalesReturnsClient from '@/components/admin/sales/sales-returns-client';

export const metadata = { title: 'Returns | Sales' };

export default async function ReturnsPage() {
  const session = await getSession();
  if (!session?.user) {
    redirect('/login');
  }

  const result = await handleListSalesInvoices({ take: 300 });
  const invoices = result.success ? result.data?.items || [] : [];
  const materials = ((await pgGetMaterials({ page: 1, pageSize: 5000 })).items || []) as any[];

  return (
    <SalesReturnsClient
      invoices={invoices}
      materials={materials}
      userId={session.user.id}
      userName={session.user.name}
    />
  );
}
