/**
 * Universal Versioning System Types
 * Pure relational structure, no JSONB storage
 */

export type EntityType = 'park' | 'ride' | 'company' | 'ride_model';

export type ChangeType = 'created' | 'updated' | 'deleted' | 'restored' | 'archived';

/**
 * Base version metadata common to all entity types
 * NOTE: Using version_id as the primary identifier (not 'id')
 * This matches the relational version table structure
 */
export interface BaseVersion {
  version_id: string;
  version_number: number;
  created_at: string;
  created_by: string | null;
  change_type: ChangeType;
  change_reason: string | null;
  submission_id: string | null;
  is_current: boolean;
}

/**
 * Extended version with profile data joined
 */
export interface BaseVersionWithProfile extends BaseVersion {
  profiles?: {
    username: string | null;
    display_name: string | null;
    avatar_url: string | null;
  } | null;
}

/**
 * Park Version - exact mirror of parks table structure
 */
export interface ParkVersion extends BaseVersionWithProfile {
  park_id: string;
  name: string;
  slug: string;
  description: string | null;
  park_type: string;
  status: string;
  location_id: string | null;
  operator_id: string | null;
  property_owner_id: string | null;
  opening_date: string | null;
  closing_date: string | null;
  opening_date_precision: string | null;
  closing_date_precision: string | null;
  website_url: string | null;
  phone: string | null;
  email: string | null;
  banner_image_url: string | null;
  banner_image_id: string | null;
  card_image_url: string | null;
  card_image_id: string | null;
}

/**
 * Ride Version - exact mirror of ride_versions table structure
 * FIXED: Added missing fields, removed JSONB former_names
 */
export interface RideVersion extends BaseVersionWithProfile {
  ride_id: string;
  name: string;
  slug: string;
  description: string | null;
  category: string;
  status: string;
  park_id: string | null;
  manufacturer_id: string | null;
  designer_id: string | null;
  ride_model_id: string | null;
  opening_date: string | null;
  closing_date: string | null;
  opening_date_precision: string | null;
  closing_date_precision: string | null;
  height_requirement_cm: number | null;
  age_requirement: number | null;
  max_speed_kmh: number | null;
  duration_seconds: number | null;
  capacity_per_hour: number | null;
  gforce_max: number | null;
  inversions_count: number | null;
  length_meters: number | null;
  height_meters: number | null;
  drop_meters: number | null;
  banner_image_url: string | null;
  banner_image_id: string | null;
  card_image_url: string | null;
  card_image_id: string | null;
  image_url: string | null;
  ride_sub_type: string | null;
  coaster_type: string | null;
  seating_type: string | null;
  intensity_level: string | null;
  track_material: string[] | null;
  support_material: string[] | null;
  propulsion_method: string[] | null;
  // Water Ride Fields
  water_depth_cm: number | null;
  splash_height_meters: number | null;
  wetness_level: string | null;
  flume_type: string | null;
  boat_capacity: number | null;
  // Dark Ride Fields
  theme_name: string | null;
  story_description: string | null;
  show_duration_seconds: number | null;
  animatronics_count: number | null;
  projection_type: string | null;
  ride_system: string | null;
  scenes_count: number | null;
  // Flat Ride Fields
  rotation_type: string | null;
  motion_pattern: string | null;
  platform_count: number | null;
  swing_angle_degrees: number | null;
  rotation_speed_rpm: number | null;
  arm_length_meters: number | null;
  max_height_reached_meters: number | null;
  // Kiddie Ride Fields
  min_age: number | null;
  max_age: number | null;
  educational_theme: string | null;
  character_theme: string | null;
  // Transportation Fields
  transport_type: string | null;
  route_length_meters: number | null;
  stations_count: number | null;
  vehicle_capacity: number | null;
  vehicles_count: number | null;
  round_trip_duration_seconds: number | null;
}

/**
 * Company Version - exact mirror of companies table structure
 */
export interface CompanyVersion extends BaseVersionWithProfile {
  company_id: string;
  name: string;
  slug: string;
  description: string | null;
  company_type: string;
  person_type: string | null;
  founded_year: number | null;
  founded_date: string | null;
  founded_date_precision: string | null;
  headquarters_location: string | null;
  website_url: string | null;
  logo_url: string | null;
  banner_image_url: string | null;
  banner_image_id: string | null;
  card_image_url: string | null;
  card_image_id: string | null;
}

/**
 * Ride Model Version - exact mirror of ride_models table structure
 */
export interface RideModelVersion extends BaseVersionWithProfile {
  ride_model_id: string;
  name: string;
  slug: string;
  manufacturer_id: string | null;
  category: string;
  ride_type: string;
  description: string | null;
  banner_image_url: string | null;
  banner_image_id: string | null;
  card_image_url: string | null;
  card_image_id: string | null;
  // Note: technical_specs removed - use ride_model_technical_specifications table
}

/**
 * Discriminated union of all entity version types
 */
export type EntityVersion = ParkVersion | RideVersion | CompanyVersion | RideModelVersion;

/**
 * Version comparison diff structure
 */
export interface VersionDiff {
  [fieldName: string]: {
    from: any;
    to: any;
  };
}

/**
 * Version summary for list display
 */
export interface VersionSummary {
  version_id: string;
  version_number: number;
  created_at: string;
  created_by: string | null;
  change_type: ChangeType;
  changed_by_username?: string;
  changed_by_display_name?: string;
  entity_name: string;
}
