import DashboardLayout from "@/components/layout/dashboard-layout";
import { verifySession } from "@/lib/auth";
import { hasEmployeePermission } from "@/lib/employee-permissions";
import { getTranslations, getCurrentLocale } from "@/lib/i18n";
import ShiftCalendar from "@/components/admin/shift-calendar";

export default async function ShiftManagementPage() {
    const session = await verifySession();
    const user = session.user;
    
    const locale = await getCurrentLocale();
    const t = getTranslations(locale);
    const tGlobal = t.Global;
    const tShiftManagement = t.ShiftManagement;

    const settings = await (await import('@/lib/postgres/data-access')).pgGetSettings();
    if (!user || !hasEmployeePermission(user, settings, 'admin.shifts')) {
        return (
            <DashboardLayout>
                <div className="text-center">
                    <h1 className="text-2xl font-bold">{tGlobal.accessDenied}</h1>
                    <p>{tGlobal.noPermission}</p>
                </div>
            </DashboardLayout>
        );
    }

    let employees;
    let shiftOverrides;
    
    const { pgGetEmployees, pgGetShiftOverrides } = await import("@/lib/postgres/data-access");
    const [employeesResult, overridesResult] = await Promise.all([
        pgGetEmployees({ page: 1, pageSize: 5000 }),
        pgGetShiftOverrides({ page: 1, pageSize: 5000 }),
    ]);
    employees = (employeesResult.items || []).filter((e) => e.role === 'employee');
    shiftOverrides = overridesResult.items || [];
    

    return (
        <DashboardLayout>
            <div className="w-full max-w-7xl mx-auto">
                <div className="grid w-full gap-2 mb-4">
                    <h1 className="text-3xl font-semibold">{tShiftManagement.title}</h1>
                    <p className="text-muted-foreground">{tShiftManagement.description}</p>
                </div>
                <ShiftCalendar employees={employees} t={tShiftManagement} shiftOverrides={shiftOverrides} />
            </div>
        </DashboardLayout>
    );
}
