/**
 * Test Data Generators
 * 
 * Factory functions for creating realistic test data.
 */

import { faker } from '@faker-js/faker';

export interface ParkTestData {
  name: string;
  slug: string;
  description: string;
  park_type: string;
  status: string;
  location_country: string;
  location_city: string;
  latitude: number;
  longitude: number;
  opened_date: string;
  opening_date_precision?: string;
  closing_date?: string;
  closing_date_precision?: string;
  source_url?: string;
  submission_notes?: string;
  is_test_data: boolean;
}

export interface RideTestData {
  name: string;
  slug: string;
  description: string;
  category: string;
  status: string;
  park_id: string;
  opened_date: string;
  opening_date_precision?: string;
  closing_date?: string;
  closing_date_precision?: string;
  track_material?: string[];
  support_material?: string[];
  propulsion_method?: string[];
  source_url?: string;
  submission_notes?: string;
  is_test_data: boolean;
  // Category-specific fields (optional)
  water_depth_cm?: number;
  splash_height_meters?: number;
  wetness_level?: string;
  flume_type?: string;
  boat_capacity?: number;
  theme_name?: string;
  story_description?: string;
  show_duration_seconds?: number;
  animatronics_count?: number;
  projection_type?: string;
  ride_system?: string;
  scenes_count?: number;
  rotation_type?: string;
  motion_pattern?: string;
  platform_count?: number;
  swing_angle_degrees?: number;
  rotation_speed_rpm?: number;
  arm_length_meters?: number;
  max_height_reached_meters?: number;
  min_age?: number;
  max_age?: number;
  educational_theme?: string;
  character_theme?: string;
  transport_type?: string;
  route_length_meters?: number;
  stations_count?: number;
  vehicle_capacity?: number;
  vehicles_count?: number;
  round_trip_duration_seconds?: number;
}

export interface CompanyTestData {
  name: string;
  slug: string;
  description: string;
  company_type: string;
  person_type: string;
  founded_date: string;
  founded_date_precision?: string;
  defunct_date?: string;
  defunct_date_precision?: string;
  source_url?: string;
  submission_notes?: string;
  is_test_data: boolean;
}

export interface RideModelTestData {
  name: string;
  slug: string;
  description: string;
  category: string;
  manufacturer_id: string;
  source_url?: string;
  submission_notes?: string;
  is_test_data: boolean;
}

/**
 * Generate random park test data
 */
export function generateParkData(overrides?: Partial<ParkTestData>): ParkTestData {
  const name = faker.company.name() + ' Park';
  const openedDate = faker.date.past({ years: 50 }).toISOString().split('T')[0];
  const status = faker.helpers.arrayElement(['operating', 'closed', 'under_construction']);
  
  const data: ParkTestData = {
    name,
    slug: faker.helpers.slugify(name).toLowerCase(),
    description: faker.lorem.paragraphs(2),
    park_type: faker.helpers.arrayElement(['theme_park', 'amusement_park', 'water_park']),
    status,
    location_country: faker.location.countryCode(),
    location_city: faker.location.city(),
    latitude: parseFloat(faker.location.latitude()),
    longitude: parseFloat(faker.location.longitude()),
    opened_date: openedDate,
    opening_date_precision: faker.helpers.arrayElement(['day', 'month', 'year']),
    is_test_data: true,
  };

  // Add closing date for closed parks
  if (status === 'closed') {
    data.closing_date = faker.date.between({ from: openedDate, to: new Date() }).toISOString().split('T')[0];
    data.closing_date_precision = faker.helpers.arrayElement(['day', 'month', 'year']);
  }

  // Add optional fields
  if (faker.datatype.boolean()) {
    data.source_url = faker.internet.url();
  }
  
  if (faker.datatype.boolean()) {
    data.submission_notes = faker.lorem.sentence();
  }

  return { ...data, ...overrides };
}

/**
 * Generate random ride test data
 */
export function generateRideData(parkId: string, overrides?: Partial<RideTestData>): RideTestData {
  const name = faker.word.adjective() + ' ' + faker.word.noun();
  const openedDate = faker.date.past({ years: 30 }).toISOString().split('T')[0];
  const status = faker.helpers.arrayElement(['operating', 'closed', 'sbno']);
  const category = faker.helpers.arrayElement(['roller_coaster', 'flat_ride', 'water_ride', 'dark_ride']);
  
  const data: RideTestData = {
    name,
    slug: faker.helpers.slugify(name).toLowerCase(),
    description: faker.lorem.paragraphs(2),
    category,
    status,
    park_id: parkId,
    opened_date: openedDate,
    opening_date_precision: faker.helpers.arrayElement(['day', 'month', 'year']),
    is_test_data: true,
  };

  // Add closing date for closed rides
  if (status === 'closed') {
    data.closing_date = faker.date.between({ from: openedDate, to: new Date() }).toISOString().split('T')[0];
    data.closing_date_precision = faker.helpers.arrayElement(['day', 'month', 'year']);
  }

  // Add material arrays for roller coasters
  if (category === 'roller_coaster' && faker.datatype.boolean()) {
    data.track_material = [faker.helpers.arrayElement(['steel', 'wood', 'hybrid'])];
    data.support_material = [faker.helpers.arrayElement(['steel', 'wood', 'concrete'])];
    data.propulsion_method = [faker.helpers.arrayElement(['chain_lift', 'cable_lift', 'launch', 'gravity'])];
  }

  // Add category-specific fields
  if (category === 'water_ride' && faker.datatype.boolean()) {
    data.water_depth_cm = faker.number.int({ min: 30, max: 300 });
    data.splash_height_meters = faker.number.float({ min: 1, max: 20, fractionDigits: 1 });
    data.wetness_level = faker.helpers.arrayElement(['dry', 'light', 'moderate', 'soaked']);
    data.flume_type = faker.helpers.arrayElement(['log', 'tube', 'raft', 'boat']);
    data.boat_capacity = faker.number.int({ min: 2, max: 20 });
  }

  if (category === 'dark_ride' && faker.datatype.boolean()) {
    data.theme_name = faker.lorem.words(2);
    data.story_description = faker.lorem.sentence();
    data.show_duration_seconds = faker.number.int({ min: 180, max: 600 });
    data.animatronics_count = faker.number.int({ min: 5, max: 50 });
    data.projection_type = faker.helpers.arrayElement(['2d', '3d', 'holographic', 'mixed']);
    data.ride_system = faker.helpers.arrayElement(['omnimover', 'tracked', 'trackless', 'boat']);
    data.scenes_count = faker.number.int({ min: 5, max: 20 });
  }

  if (category === 'flat_ride' && faker.datatype.boolean()) {
    data.rotation_type = faker.helpers.arrayElement(['horizontal', 'vertical', 'both', 'none']);
    data.motion_pattern = faker.helpers.arrayElement(['circular', 'pendulum', 'spinning', 'wave']);
    data.platform_count = faker.number.int({ min: 1, max: 8 });
    data.swing_angle_degrees = faker.number.int({ min: 45, max: 180 });
    data.rotation_speed_rpm = faker.number.int({ min: 5, max: 30 });
    data.arm_length_meters = faker.number.float({ min: 5, max: 25, fractionDigits: 1 });
    data.max_height_reached_meters = faker.number.float({ min: 10, max: 80, fractionDigits: 1 });
  }

  // Add optional fields
  if (faker.datatype.boolean()) {
    data.source_url = faker.internet.url();
  }
  
  if (faker.datatype.boolean()) {
    data.submission_notes = faker.lorem.sentence();
  }

  return { ...data, ...overrides };
}

/**
 * Generate random company test data
 */
export function generateCompanyData(
  companyType: 'manufacturer' | 'designer' | 'operator' | 'property_owner',
  overrides?: Partial<CompanyTestData>
): CompanyTestData {
  const name = faker.company.name();
  const foundedDate = faker.date.past({ years: 100 }).toISOString().split('T')[0];
  
  const data: CompanyTestData = {
    name,
    slug: faker.helpers.slugify(name).toLowerCase(),
    description: faker.lorem.paragraphs(2),
    company_type: companyType,
    person_type: faker.helpers.arrayElement(['individual', 'company']),
    founded_date: foundedDate,
    founded_date_precision: faker.helpers.arrayElement(['day', 'month', 'year']),
    is_test_data: true,
  };

  // Add defunct date for some companies
  if (faker.datatype.boolean(0.15)) {
    data.defunct_date = faker.date.between({ from: foundedDate, to: new Date() }).toISOString().split('T')[0];
    data.defunct_date_precision = faker.helpers.arrayElement(['day', 'month', 'year']);
  }

  // Add optional fields
  if (faker.datatype.boolean()) {
    data.source_url = faker.internet.url();
  }
  
  if (faker.datatype.boolean()) {
    data.submission_notes = faker.lorem.sentence();
  }

  return { ...data, ...overrides };
}

/**
 * Generate random ride model test data
 */
export function generateRideModelData(
  manufacturerId: string,
  overrides?: Partial<RideModelTestData>
): RideModelTestData {
  const name = faker.word.adjective() + ' Model';
  
  const data: RideModelTestData = {
    name,
    slug: faker.helpers.slugify(name).toLowerCase(),
    description: faker.lorem.paragraphs(2),
    category: faker.helpers.arrayElement(['roller_coaster', 'flat_ride', 'water_ride']),
    manufacturer_id: manufacturerId,
    is_test_data: true,
  };

  // Add optional fields
  if (faker.datatype.boolean()) {
    data.source_url = faker.internet.url();
  }
  
  if (faker.datatype.boolean()) {
    data.submission_notes = faker.lorem.sentence();
  }

  return { ...data, ...overrides };
}

/**
 * Generate unique test identifier
 */
export function generateTestId(): string {
  return `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
