import { redirect } from 'next/navigation';
import { getTranslations } from 'next-intl/server';
import type { Metadata } from 'next';
import { resolveUser } from '@/lib/auth';
import { QualityConsole } from './quality-console';

export async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations('quality');
  return { title: t('documentTitle') };
}

export default async function QualityPage() {
  const user = await resolveUser();
  // Only quality / admin / supervisor may view the console; everyone else out.
  if (user && !['QUALITY', 'ADMIN', 'SUPERVISOR'].includes(user.role)) {
    redirect('/maintenance');
  }
  // Defects are raised by QCP and admins; supervisors get a read-only view.
  const canCreate = user?.role === 'QUALITY' || user?.role === 'ADMIN';

  return <QualityConsole canCreate={canCreate} />;
}
