/**
 * Certificate of Authenticity API
 * GET /api/transparency/certificate/[plantId] - Generate certificate for a plant
 *
 * Provides verifiable certificates of plant authenticity and provenance.
 */

import type { NextApiRequest, NextApiResponse } from 'next';
import { getSignatureManager } from '../../../../lib/transparency';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== 'GET') {
    return res.status(405).json({ error: 'Method not allowed' });
  }

  const { plantId, format = 'json' } = req.query;

  if (!plantId || typeof plantId !== 'string') {
    return res.status(400).json({
      success: false,
      error: 'Missing plant ID'
    });
  }

  try {
    const signatureManager = getSignatureManager();
    const certificate = await signatureManager.generateCertificate(plantId);

    if (!certificate) {
      return res.status(404).json({
        success: false,
        error: 'Plant not found or certificate generation failed'
      });
    }

    // Verify the certificate
    const verification = signatureManager.verifyCertificate(certificate);

    if (format === 'text') {
      res.setHeader('Content-Type', 'text/plain');
      return res.status(200).send(`
CERTIFICATE OF AUTHENTICITY
===========================
Certificate ID: ${certificate.id}
Generated: ${certificate.generatedAt}

PLANT INFORMATION
-----------------
Plant ID: ${certificate.plantId}
Name: ${certificate.plantName}
Variety: ${certificate.variety}

ORIGIN
------
Grower: ${certificate.origin.grower}
Location: ${certificate.origin.location}
Registered: ${certificate.origin.registeredAt}

LINEAGE
-------
Generation: ${certificate.lineage.generation}
Parent ID: ${certificate.lineage.parentId || 'None (Original)'}
Children: ${certificate.lineage.childCount}

SIGNATURES (${certificate.signatures.length})
--------------------------------------------
${certificate.signatures.map(s =>
  `[${s.verified ? 'VERIFIED' : 'UNVERIFIED'}] ${s.type} by ${s.signer} at ${s.timestamp}`
).join('\n') || 'No signatures recorded'}

TRANSPORT HISTORY (${certificate.transportHistory.length})
---------------------------------------------------------
${certificate.transportHistory.map(t =>
  `${t.date}: ${t.from} -> ${t.to} [${t.verified ? 'Verified' : 'Unverified'}]`
).join('\n') || 'No transport history recorded'}

CERTIFICATIONS
--------------
${certificate.certifications.join('\n') || 'No certifications'}

VERIFICATION STATUS
-------------------
Certificate Valid: ${verification.valid ? 'YES' : 'NO'}
${verification.errors.length > 0 ? `Errors:\n${verification.errors.map(e => `  - ${e}`).join('\n')}` : ''}

Certificate Hash: ${certificate.hash}

This certificate was generated by LocalGreenChain transparency system.
Verify at: /api/transparency/certificate/${certificate.plantId}
      `.trim());
    }

    return res.status(200).json({
      success: true,
      data: {
        certificate,
        verification
      }
    });
  } catch (error) {
    console.error('[API] Certificate error:', error);
    return res.status(500).json({
      success: false,
      error: 'Failed to generate certificate'
    });
  }
}
