/**
 * Client-side validation for entity submissions
 * Prevents missing required fields before database calls
 */

export interface ValidationResult {
  valid: boolean;
  missingFields: string[];
  errorMessage?: string;
}

/**
 * Validates required fields for park creation
 */
export function validateParkCreateFields(data: any): ValidationResult {
  const missingFields: string[] = [];
  
  if (!data.name?.trim()) missingFields.push('name');
  if (!data.slug?.trim()) missingFields.push('slug');
  if (!data.park_type) missingFields.push('park_type');
  if (!data.status) missingFields.push('status');
  
  if (missingFields.length > 0) {
    return {
      valid: false,
      missingFields,
      errorMessage: `Missing required fields for park creation: ${missingFields.join(', ')}`
    };
  }
  
  return { valid: true, missingFields: [] };
}

/**
 * Validates required fields for ride creation
 */
export function validateRideCreateFields(data: any): ValidationResult {
  const missingFields: string[] = [];
  
  if (!data.name?.trim()) missingFields.push('name');
  if (!data.slug?.trim()) missingFields.push('slug');
  if (!data.category) missingFields.push('category');
  if (!data.status) missingFields.push('status');
  
  if (missingFields.length > 0) {
    return {
      valid: false,
      missingFields,
      errorMessage: `Missing required fields for ride creation: ${missingFields.join(', ')}`
    };
  }
  
  return { valid: true, missingFields: [] };
}

/**
 * Validates required fields for company creation
 */
export function validateCompanyCreateFields(data: any): ValidationResult {
  const missingFields: string[] = [];
  
  if (!data.name?.trim()) missingFields.push('name');
  if (!data.slug?.trim()) missingFields.push('slug');
  if (!data.company_type) missingFields.push('company_type');
  
  if (missingFields.length > 0) {
    return {
      valid: false,
      missingFields,
      errorMessage: `Missing required fields for company creation: ${missingFields.join(', ')}`
    };
  }
  
  return { valid: true, missingFields: [] };
}

/**
 * Validates required fields for ride model creation
 */
export function validateRideModelCreateFields(data: any): ValidationResult {
  const missingFields: string[] = [];
  
  if (!data.name?.trim()) missingFields.push('name');
  if (!data.slug?.trim()) missingFields.push('slug');
  if (!data.manufacturer_id) missingFields.push('manufacturer_id');
  if (!data.category) missingFields.push('category');
  
  if (missingFields.length > 0) {
    return {
      valid: false,
      missingFields,
      errorMessage: `Missing required fields for ride model creation: ${missingFields.join(', ')}`
    };
  }
  
  return { valid: true, missingFields: [] };
}

/**
 * Helper to throw validation error if validation fails
 */
export function assertValid(result: ValidationResult): void {
  if (!result.valid) {
    throw new Error(result.errorMessage);
  }
}
