/**
 * @ai-summary Type definitions for CameraCapture component
 * @ai-context Defines props, state, and configuration types for camera capture functionality
 */

/** Guidance overlay type determining aspect ratio and visual hints */
export type GuidanceType = 'vin' | 'receipt' | 'document' | 'none';

/** Camera permission state */
export type PermissionState = 'prompt' | 'granted' | 'denied' | 'unavailable';

/** Camera capture workflow state */
export type CaptureState = 'idle' | 'viewfinder' | 'captured' | 'cropping';

/** Camera facing mode */
export type FacingMode = 'user' | 'environment';

/** Props for the main CameraCapture component */
export interface CameraCaptureProps {
  /** Callback when capture is complete with original file and optional cropped file */
  onCapture: (file: File, croppedFile?: File) => void;
  /** Callback when user cancels capture */
  onCancel: () => void;
  /** Type of guidance overlay to display */
  guidanceType?: GuidanceType;
  /** Whether to show crop tool after capture */
  allowCrop?: boolean;
  /** Maximum file size in bytes (default 10MB) */
  maxFileSize?: number;
  /** Accepted MIME types */
  acceptedFormats?: string[];
}

/** Props for CameraViewfinder component */
export interface CameraViewfinderProps {
  /** Currently active media stream */
  stream: MediaStream | null;
  /** Guidance overlay type */
  guidanceType: GuidanceType;
  /** Callback when capture button is pressed */
  onCapture: () => void;
  /** Callback when user cancels */
  onCancel: () => void;
  /** Callback to switch camera */
  onSwitchCamera: () => void;
  /** Whether front/rear camera switch is available */
  canSwitchCamera: boolean;
  /** Current camera facing mode */
  facingMode: FacingMode;
}

/** Props for GuidanceOverlay component */
export interface GuidanceOverlayProps {
  /** Type of guidance overlay */
  type: GuidanceType;
}

/** Props for CropTool component */
export interface CropToolProps {
  /** Source image to crop */
  imageSrc: string;
  /** Whether to lock aspect ratio */
  lockAspectRatio?: boolean;
  /** Initial crop area matching guidance overlay position */
  initialCrop?: CropArea;
  /** Aspect ratio to lock to (width/height) */
  aspectRatio?: number;
  /** Callback when crop is confirmed */
  onConfirm: (croppedBlob: Blob) => void;
  /** Callback to reset crop */
  onReset: () => void;
  /** Callback to cancel and retake */
  onRetake: () => void;
  /** Callback when user skips cropping */
  onSkip: () => void;
}

/** Props for FileInputFallback component */
export interface FileInputFallbackProps {
  /** Callback when file is selected */
  onFileSelect: (file: File) => void;
  /** Callback when user cancels */
  onCancel: () => void;
  /** Accepted MIME types */
  acceptedFormats: string[];
  /** Maximum file size in bytes */
  maxFileSize: number;
}

/** Crop area coordinates and dimensions */
export interface CropArea {
  x: number;
  y: number;
  width: number;
  height: number;
}

/** Guidance overlay aspect ratio configuration */
export interface GuidanceConfig {
  aspectRatio: number;
  label: string;
  description: string;
}

/** Guidance configurations by type */
export const GUIDANCE_CONFIGS: Record<Exclude<GuidanceType, 'none'>, GuidanceConfig> = {
  vin: {
    aspectRatio: 6,
    label: 'VIN',
    description: 'Position VIN plate within the guide',
  },
  receipt: {
    aspectRatio: 2 / 3,
    label: 'Receipt',
    description: 'Position receipt within the guide',
  },
  document: {
    aspectRatio: 8.5 / 11,
    label: 'Document',
    description: 'Position document within the guide',
  },
};

/** Default accepted formats */
export const DEFAULT_ACCEPTED_FORMATS = [
  'image/jpeg',
  'image/png',
  'image/heic',
  'image/heif',
  'application/pdf',
];

/** Default max file size (10MB) */
export const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;

/** Clamps a value to the 0-100 percentage range */
function clampPct(value: number): number {
  return Math.max(0, Math.min(100, value));
}

/** Derives centered CropArea matching GuidanceOverlay position for each type */
export function getInitialCropForGuidance(type: GuidanceType): CropArea | undefined {
  if (type === 'none') return undefined;

  const config = GUIDANCE_CONFIGS[type];
  let width: number;
  let height: number;

  if (config.aspectRatio >= 1) {
    // Width-constrained (VIN, square): 85% width, height from AR, centered
    width = 85;
    height = width / config.aspectRatio;
  } else {
    // Height-constrained (receipt, document): 70% height, width from AR, centered
    height = 70;
    width = height * config.aspectRatio;
  }

  return {
    x: clampPct((100 - width) / 2),
    y: clampPct((100 - height) / 2),
    width: clampPct(width),
    height: clampPct(height),
  };
}
