import { notFound } from 'next/navigation';
import PageContainer from '@/components/layout/page-container';
import EmployeeForm from '@/features/employees/components/employee-form';
import {
  getEmployeeByIdForOrganization,
  serializeEmployee
} from '@/features/employees/server/employee-data';
import { requireHRDDashboardAccess } from '@/lib/auth/page-guards';

export const metadata = {
  title: 'Dashboard: Edit Employee'
};

type PageProps = {
  params: Promise<{ id: string }>;
};

export default async function Page(props: PageProps) {
  const access = await requireHRDDashboardAccess();
  const params = await props.params;
  const employee = await getEmployeeByIdForOrganization({
    id: Number(params.id),
    organizationId: access.organization.id
  });

  if (!employee) {
    notFound();
  }

  return (
    <PageContainer pageTitle='แก้ไขข้อมูลพนักงาน' pageDescription='ปรับปรุงข้อมูล Employee Master'>
      <EmployeeForm
        initialData={serializeEmployee(employee)}
        pageTitle='แก้ไขข้อมูลพนักงาน'
        organizationName={access.organization.name}
        returnPath='/dashboard/employee-directory'
      />
    </PageContainer>
  );
}
