import type { FaceValidationPort, FaceValidationResult } from '@core/media';

/**
 * Demo implementation of FaceValidationPort.
 *
 * In production, this would use a real face detection API like:
 * - AWS Rekognition
 * - Google Cloud Vision
 * - Azure Face API
 * - OpenCV / face-api.js
 *
 * For demo purposes, this always returns a valid face if the image data is provided.
 */
export class DemoFaceValidationAdapter implements FaceValidationPort {
  async validateFacePhoto(imageData: string | Buffer): Promise<FaceValidationResult> {
    // Simulate some processing time
    await this.delay(500);

    // Check if we have any image data
    const dataString = typeof imageData === 'string' ? imageData : imageData.toString();

    if (!dataString || dataString.length < 100) {
      return {
        isValid: false,
        hasFace: false,
        faceCount: 0,
        confidence: 0,
        errorMessage: 'Invalid or empty image data',
      };
    }

    // Check for valid base64 image data or data URL
    const isValidImage =
      dataString.startsWith('data:image/') ||
      dataString.startsWith('/9j/') || // JPEG magic bytes in base64
      dataString.startsWith('iVBOR') || // PNG magic bytes in base64
      dataString.length > 1000; // Assume long strings are valid image data

    if (!isValidImage) {
      return {
        isValid: false,
        hasFace: false,
        faceCount: 0,
        confidence: 0,
        errorMessage: 'Please upload a valid image file (JPEG or PNG)',
      };
    }

    // For demo: always return success with high confidence
    // In production, this would actually analyze the image
    return {
      isValid: true,
      hasFace: true,
      faceCount: 1,
      confidence: 0.95,
    };
  }

  private delay(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
}