import { ERROR_MESSAGES, ERROR_CODES } from '../constants/errors';
import { GENERATION_LIMITS } from '../constants/limits';

export interface ValidationResult {
  valid: boolean;
  error?: {
    message: string;
    code: string;
  };
}

export const validateUUID = (id: string): ValidationResult => {
  const uuidPattern =
    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

  if (!uuidPattern.test(id)) {
    return {
      valid: false,
      error: {
        message: ERROR_MESSAGES.INVALID_UUID,
        code: ERROR_CODES.INVALID_UUID,
      },
    };
  }

  return { valid: true };
};

export const validateAspectRatio = (aspectRatio: string): ValidationResult => {
  if (!GENERATION_LIMITS.ALLOWED_ASPECT_RATIOS.includes(aspectRatio as any)) {
    return {
      valid: false,
      error: {
        message: `Invalid aspect ratio. Allowed values: ${GENERATION_LIMITS.ALLOWED_ASPECT_RATIOS.join(', ')}`,
        code: ERROR_CODES.INVALID_ASPECT_RATIO,
      },
    };
  }

  return { valid: true };
};

export const validateFocalPoint = (focalPoint: {
  x: number;
  y: number;
}): ValidationResult => {
  if (
    focalPoint.x < 0 ||
    focalPoint.x > 1 ||
    focalPoint.y < 0 ||
    focalPoint.y > 1
  ) {
    return {
      valid: false,
      error: {
        message: ERROR_MESSAGES.INVALID_FOCAL_POINT,
        code: ERROR_CODES.INVALID_FOCAL_POINT,
      },
    };
  }

  return { valid: true };
};

export const validateDateRange = (
  startDate?: string,
  endDate?: string
): ValidationResult => {
  if (startDate && isNaN(Date.parse(startDate))) {
    return {
      valid: false,
      error: {
        message: 'Invalid start date format',
        code: ERROR_CODES.VALIDATION_ERROR,
      },
    };
  }

  if (endDate && isNaN(Date.parse(endDate))) {
    return {
      valid: false,
      error: {
        message: 'Invalid end date format',
        code: ERROR_CODES.VALIDATION_ERROR,
      },
    };
  }

  if (startDate && endDate && new Date(startDate) > new Date(endDate)) {
    return {
      valid: false,
      error: {
        message: 'Start date must be before end date',
        code: ERROR_CODES.VALIDATION_ERROR,
      },
    };
  }

  return { valid: true };
};
