export interface Location {
  id: string;
  name: string;
  country: string;
  state_province?: string | null;
  city?: string | null;
  postal_code?: string | null;
  latitude?: number | null;
  longitude?: number | null;
  timezone?: string | null;
}

export interface Company {
  id: string;
  name: string;
  slug: string;
  description?: string | null;
  company_type: string; // Allow any string from database
  person_type?: string | null; // Database returns string, validated at form level
  website_url?: string | null;
  founded_year?: number | null; // Legacy field
  founded_date?: string | null;
  founded_date_precision?: string | null;
  headquarters_location?: 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;
  average_rating: number | null;
  review_count: number | null;
}

export interface Park {
  id: string;
  name: string;
  slug: string;
  description?: string | null;
  status: string; // Allow any string from database
  park_type: string; // Allow any string from database
  opening_date?: string | null;
  opening_date_precision?: string | null;
  closing_date?: string | null;
  closing_date_precision?: string | null;
  website_url?: string | null;
  phone?: string | null;
  email?: string | null;
  location?: Location | null;
  operator?: Company | null;
  property_owner?: Company | null;
  banner_image_url?: string | null;
  banner_image_id?: string | null;
  card_image_url?: string | null;
  card_image_id?: string | null;
  average_rating: number | null;
  review_count: number | null;
  ride_count: number | null;
  coaster_count: number | null;
  created_at: string;
  updated_at: string;
}

export interface RideTechnicalSpec {
  id: string;
  ride_id: string;
  spec_name: string;
  spec_value: string;
  spec_type: 'string' | 'number' | 'boolean' | 'date';
  category?: string;
  unit?: string;
  display_order: number;
  created_at: string;
}

export interface RideCoasterStat {
  id: string;
  ride_id: string;
  stat_name: string;
  stat_value: number;
  unit?: string;
  category?: string;
  description?: string;
  display_order: number;
  created_at: string;
}

export interface RideNameHistory {
  id: string;
  ride_id: string;
  former_name: string;
  date_changed?: string;
  date_changed_precision?: string;
  reason?: string;
  from_year?: number;
  to_year?: number;
  order_index: number;
  created_at: string;
}

export interface RideModelTechnicalSpec {
  id: string;
  ride_model_id: string;
  spec_name: string;
  spec_value: string;
  spec_type: 'string' | 'number' | 'boolean' | 'date';
  category?: string;
  unit?: string;
  display_order: number;
  created_at: string;
}

export interface RideModel {
  id: string;
  name: string;
  slug: string;
  manufacturer_id: string;
  manufacturer?: Company;
  category: 'roller_coaster' | 'flat_ride' | 'water_ride' | 'dark_ride' | 'kiddie_ride' | 'transportation';
  ride_type?: string;
  description?: string;
  // Note: technical_specs deprecated - use ride_model_technical_specifications table
  // Load via useTechnicalSpecifications hook instead
  banner_image_url?: string;
  banner_image_id?: string;
  card_image_url?: string;
  card_image_id?: string;
  created_at?: string;
  updated_at?: string;
}

export interface Ride {
  id: string;
  name: string;
  slug: string;
  description?: string | null;
  park?: Park | { name: string; slug: string; location?: Location }; // Allow partial park data
  ride_model?: RideModel;
  manufacturer?: Company | null;
  designer?: Company | null;
  category: string; // Allow any string from database
  ride_sub_type?: string | null; // Sub-category like "Flying Coaster", "Inverted Coaster", etc.
  status: string; // Allow any string from database
  opening_date?: string | null;
  opening_date_precision?: string | null;
  closing_date?: string | null;
  closing_date_precision?: string | null;
  height_requirement?: number | null;
  age_requirement?: number | null;
  capacity_per_hour?: number | null;
  duration_seconds?: number | null;
  max_speed_kmh?: number | null;
  max_height_meters?: number | null;
  length_meters?: number | null;
  inversions?: number | null;
  technical_specifications?: RideTechnicalSpec[];
  coaster_statistics?: RideCoasterStat[];
  name_history?: RideNameHistory[];
  average_rating: number | null;
  review_count: number | null;
  image_url?: string | null;
  banner_image_url?: string | null;
  banner_image_id?: string | null;
  card_image_url?: string | null;
  card_image_id?: string | null;
  // Roller coaster specific fields
  coaster_type?: string | null;
  seating_type?: string | null;
  intensity_level?: string | null;
  track_material?: string[] | null;
  support_material?: string[] | null;
  propulsion_method?: string[] | null;
  drop_height_meters?: number | null;
  max_g_force?: number | null;
  // Water ride specific 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 specific 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 specific 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 specific fields
  min_age?: number | null;
  max_age?: number | null;
  educational_theme?: string | null;
  character_theme?: string | null;
  // Transportation specific 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;
}

export interface Profile {
  id: string;
  user_id: string;
  username: string;
  display_name?: string;
  bio?: string;
  avatar_url?: string;
  avatar_image_id?: string;
  preferred_pronouns?: string;
  show_pronouns?: boolean;
  timezone?: string;
  preferred_language?: string;
  location?: Location;
  location_id?: string;
  personal_location?: string;
  home_park_id?: string;
  date_of_birth?: string;
  privacy_level: 'public' | 'friends' | 'private';
  theme_preference: 'light' | 'dark' | 'system';
  ride_count: number;
  coaster_count: number;
  park_count: number;
  review_count: number;
  reputation_score: number;
  banned: boolean;
  deactivated: boolean;
  deactivated_at?: string;
  deactivation_reason?: string;
  oauth_provider?: string;
  created_at: string;
  updated_at: string;
}

export interface AccountDeletionRequest {
  id: string;
  user_id: string;
  requested_at: string;
  scheduled_deletion_at: string;
  confirmation_code: string;
  confirmation_code_sent_at?: string;
  status: 'pending' | 'confirmed' | 'cancelled' | 'completed'; // Account deletion specific status
  cancelled_at?: string;
  completed_at?: string;
  cancellation_reason?: string;
  created_at: string;
  updated_at: string;
}

export interface Review {
  id: string;
  user_id: string;
  park?: Park;
  ride?: Ride;
  rating: number;
  title?: string;
  content?: string;
  visit_date?: string;
  wait_time_minutes?: number;
  photos?: any;
  helpful_votes: number;
  total_votes: number;
  moderation_status: 'pending' | 'approved' | 'rejected' | 'flagged'; // Review moderation status
  created_at: string;
  updated_at: string;
}

export interface UserTopList {
  id: string;
  user_id: string;
  title: string;
  description?: string | null;
  list_type: 'parks' | 'rides' | 'coasters' | 'companies' | 'mixed';
  is_public: boolean | null;
  created_at: string;
  updated_at: string;
  items?: UserTopListItem[]; // New relational data
}

export interface UserTopListItem {
  id: string;
  list_id: string;
  entity_type: 'park' | 'ride' | 'company';
  entity_id: string;
  position: number;
  notes?: string | null;
  created_at: string;
  updated_at: string;
  // Populated via joins
  entity?: Park | Ride | Company;
}

// Extended company interface with aggregated stats
export interface CompanyWithStats extends Company {
  ride_count?: number;
  coaster_count?: number;
  model_count?: number;
  park_count?: number;
}

// Audit log entry - matches actual profile_audit_log table structure
export interface AuditLogEntry {
  id: string;
  user_id: string;
  changed_by: string;
  action: string;
  changes: Record<string, unknown> | null;
  ip_address_hash: string;
  user_agent: string;
  created_at: string;
}

// Relational data structures (NO JSONB)
export interface RideCoasterStat {
  id: string;
  ride_id: string;
  stat_name: string;
  stat_value: number;
  unit?: string;
  category?: string;
  description?: string;
  display_order: number;
  created_at: string;
  updated_at: string;
}

export interface RideTechnicalSpecification {
  id: string;
  ride_id: string;
  spec_name: string;
  spec_value: string;
  spec_type: string;
  category?: string | null;
  unit?: string | null;
  display_order: number;
  created_at: string;
}

export interface RideModelTechnicalSpecification {
  id: string;
  ride_model_id: string;
  spec_name: string;
  spec_value: string;
  spec_type: string;
  category?: string | null;
  unit?: string | null;
  display_order: number;
  created_at: string;
}

export interface ListItem {
  id: string;
  list_id: string;
  entity_type: 'park' | 'ride' | 'coaster';
  entity_id: string;
  position: number;
  notes?: string | null;
  created_at: string;
  updated_at: string;
}

// User ride credit tracking
export interface UserRideCredit {
  id: string;
  user_id: string;
  ride_id: string;
  first_ride_date?: string;
  last_ride_date?: string;
  ride_count: number;
  sort_order?: number;
  personal_notes?: string;
  personal_rating?: number;
  personal_photo_id?: string;
  created_at: string;
  updated_at: string;
  rides?: {
    id: string;
    name: string;
    slug: string;
    category: string;
    status: string;
    coaster_type?: string;
    seating_type?: string;
    intensity_level?: string;
    track_material?: string;
    max_speed_kmh?: number;
    max_height_meters?: number;
    length_meters?: number;
    inversions?: number;
    card_image_url?: string;
    average_rating: number;
    review_count: number;
    parks?: {
      id: string;
      name: string;
      slug: string;
      park_type: string;
      locations?: {
        country: string;
        state_province?: string;
        city?: string;
      };
    };
    manufacturer?: {
      id: string;
      name: string;
    };
    designer?: {
      id: string;
      name: string;
    };
  };
}

// Activity entry - discriminated union for different activity types
export type ActivityEntry = 
  | ReviewActivity
  | SubmissionActivity
  | RankingActivity
  | GenericActivity;

export interface ReviewActivity {
  id: string;
  type: 'review';
  created_at: string;
  rating?: number;
  parks?: { slug?: string; name?: string } | null;
  rides?: { slug?: string; name?: string; parks?: { slug?: string; name?: string } | null } | null;
  content?: string;
  title?: string;
  description?: string;
}

export interface SubmissionActivity {
  id: string;
  type: 'submission';
  created_at: string;
  status?: string;
  submission_type?: string;
  entity_type?: string;
  action?: string;
  content?: {
    name?: string;
    slug?: string;
    description?: string;
    entity_id?: string;
    entity_slug?: string;
    park_slug?: string;
    park_name?: string;
    action?: string;
  };
  photo_count?: number;
  photo_preview?: string;
}

export interface RankingActivity {
  id: string;
  type: 'ranking';
  created_at: string;
  title?: string;
  description?: string;
  list_type?: string;
  is_public?: boolean;
}

export interface GenericActivity {
  id: string;
  type?: string;
  created_at: string;
  [key: string]: unknown;
}