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

export interface Company {
  id: string;
  name: string;
  slug: string;
  description?: string;
  company_type: string; // Allow any string from database
  person_type?: string; // Database returns string, validated at form level
  website_url?: string;
  founded_year?: number; // Legacy field
  founded_date?: string;
  founded_date_precision?: string;
  headquarters_location?: string;
  logo_url?: string;
  banner_image_url?: string;
  banner_image_id?: string;
  card_image_url?: string;
  card_image_id?: string;
  average_rating: number;
  review_count: number;
}

export interface Park {
  id: string;
  name: string;
  slug: string;
  description?: string;
  status: string; // Allow any string from database
  park_type: string; // Allow any string from database
  opening_date?: string;
  opening_date_precision?: string;
  closing_date?: string;
  closing_date_precision?: string;
  website_url?: string;
  phone?: string;
  email?: string;
  location?: Location;
  operator?: Company;
  property_owner?: Company;
  banner_image_url?: string;
  banner_image_id?: string;
  card_image_url?: string;
  card_image_id?: string;
  average_rating: number;
  review_count: number;
  ride_count: number;
  coaster_count: number;
  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;
  park?: Park | { name: string; slug: string; location?: Location }; // Allow partial park data
  ride_model?: RideModel;
  manufacturer?: Company;
  designer?: Company;
  category: string; // Allow any string from database
  ride_sub_type?: string; // Sub-category like "Flying Coaster", "Inverted Coaster", etc.
  status: string; // Allow any string from database
  opening_date?: string;
  opening_date_precision?: string;
  closing_date?: string;
  closing_date_precision?: string;
  height_requirement?: number;
  age_requirement?: number;
  capacity_per_hour?: number;
  duration_seconds?: number;
  max_speed_kmh?: number;
  max_height_meters?: number;
  length_meters?: number;
  inversions?: number;
  technical_specifications?: RideTechnicalSpec[];
  coaster_statistics?: RideCoasterStat[];
  name_history?: RideNameHistory[];
  average_rating: number;
  review_count: number;
  image_url?: string;
  banner_image_url?: string;
  banner_image_id?: string;
  card_image_url?: string;
  card_image_id?: string;
  // New roller coaster specific fields
  coaster_type?: string;
  seating_type?: string;
  intensity_level?: string;
  track_material?: string;
  drop_height_meters?: number;
  max_g_force?: number;
}

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;
  list_type: 'parks' | 'rides' | 'coasters' | 'companies' | 'mixed';
  is_public: boolean;
  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;
  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 | null;
  category?: string | null;
  description?: string | null;
  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;
}