import type { SearchParams } from 'nuqs/server';
import Link from 'next/link';
import PageContainer from '@/components/layout/page-container';
import { Icons } from '@/components/icons';
import { Button } from '@/components/ui/button';
import AnnouncementsListingPage from '@/features/announcements/components/announcements-listing';
import { hasAnyPermission } from '@/lib/auth/authorization';
import { requireEmployeeDashboardAccess } from '@/lib/auth/page-guards';
import { searchParamsCache } from '@/lib/searchparams';

export const metadata = {
  title: 'Dashboard : Announcements'
};

type PageProps = {
  searchParams: Promise<SearchParams>;
};

export default async function Page(props: PageProps) {
  const access = await requireEmployeeDashboardAccess();
  const searchParams = await props.searchParams;
  searchParamsCache.parse(searchParams);

  const permissions = access.session.user.effectivePermissions;
  const canCreateAnnouncements = hasAnyPermission(permissions, ['announcement:create']);

  return (
    <PageContainer
      pageTitle='ประกาศ'
      pageDescription='ติดตามประกาศสำคัญขององค์กร พร้อมจัดการร่างประกาศ การเผยแพร่ และการปักหมุด'
      pageHeaderAction={
        canCreateAnnouncements ? (
          <Button asChild>
            <Link href='/dashboard/announcements/new'>
              <Icons.add className='h-4 w-4' />
              สร้างประกาศ
            </Link>
          </Button>
        ) : null
      }
    >
      <AnnouncementsListingPage
        currentUserId={access.session.user.id}
        permissions={permissions}
      />
    </PageContainer>
  );
}
