// packages/functions/src/vercel/api/crud/delete.ts

/**
 * @fileoverview Delete entity API handler
 * @description Vercel API route for deleting entities
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */

import type { GetEntityData } from '@donotdev/core/server';

import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';

import { handleError } from '../../../shared/errorHandling.js';
import { assertAuthenticated } from '../../../shared/utils.js';

import type { NextApiRequest, NextApiResponse } from 'next';

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

  try {
    // Verify authentication
    const uid = assertAuthenticated(req.headers.authorization);

    const { schema, id } = req.body as { schema: any; id: string };

    if (!schema || !id) {
      throw handleError(new Error('Missing schema or id'));
    }

    // Check if document exists
    const db = getFirebaseAdminFirestore();
    const doc = await db.collection(schema.metadata.collection).doc(id).get();

    if (!doc.exists) {
      throw handleError(new Error('Document not found'));
    }

    // Delete the document from Firestore
    await db.collection(schema.metadata.collection).doc(id).delete();

    return res.status(200).json({
      success: true,
      message: 'Document deleted successfully',
      id,
    });
  } catch (error) {
    throw handleError(error);
  }
}
