import fs from 'fs';
import path from 'path';
import { notFound } from 'next/navigation';
import QuotationViewerClient from './QuotationViewerClient';

interface PageProps {
  params: {
    id: string;
  };
}

function getQuoteRecord(quoteNo: string) {
  const filePath = path.join(process.cwd(), 'data', 'quotes', `${quoteNo}.json`);
  if (!fs.existsSync(filePath)) {
    return null;
  }
  try {
    const raw = fs.readFileSync(filePath, 'utf8');
    return JSON.parse(raw);
  } catch (e) {
    return null;
  }
}

export default function QuotationPage({ params }: PageProps) {
  const quoteNo = params.id;
  const record = getQuoteRecord(quoteNo);

  if (!record) {
    notFound();
  }

  const now = Date.now();
  const isExpired = now > record.expiresAt;

  return (
    <QuotationViewerClient record={record} isExpired={isExpired} />
  );
}
