-- Create category-specific detail tables for rides

-- Water Ride Details Table
CREATE TABLE IF NOT EXISTS public.ride_water_details (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  water_depth_cm DECIMAL(10,2),
  splash_height_meters DECIMAL(10,2),
  wetness_level TEXT CHECK (wetness_level IN ('dry', 'light', 'moderate', 'soaked')),
  flume_type TEXT,
  boat_capacity INTEGER,
  water_volume_liters DECIMAL(15,2),
  pump_capacity_lph DECIMAL(15,2),
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(ride_id)
);

-- Dark Ride Details Table
CREATE TABLE IF NOT EXISTS public.ride_dark_details (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  theme_name TEXT,
  story_description TEXT,
  show_duration_seconds INTEGER,
  effects_type TEXT[],
  animatronics_count INTEGER,
  projection_type TEXT,
  ride_system TEXT,
  scenes_count INTEGER,
  interactive BOOLEAN DEFAULT false,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(ride_id)
);

-- Flat Ride Details Table
CREATE TABLE IF NOT EXISTS public.ride_flat_details (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  rotation_type TEXT CHECK (rotation_type IN ('horizontal', 'vertical', 'multi_axis', 'pendulum', 'none')),
  motion_pattern TEXT,
  platform_count INTEGER,
  swing_angle_degrees DECIMAL(10,2),
  rotation_speed_rpm DECIMAL(10,2),
  arm_length_meters DECIMAL(10,2),
  max_height_reached_meters DECIMAL(10,2),
  spinning BOOLEAN DEFAULT false,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(ride_id)
);

-- Kiddie Ride Details Table
CREATE TABLE IF NOT EXISTS public.ride_kiddie_details (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  min_age INTEGER,
  max_age INTEGER,
  parent_supervision_required BOOLEAN DEFAULT false,
  educational_theme TEXT,
  character_theme TEXT,
  gentle_mode BOOLEAN DEFAULT true,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(ride_id)
);

-- Transportation Ride Details Table
CREATE TABLE IF NOT EXISTS public.ride_transportation_details (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  transport_type TEXT CHECK (transport_type IN ('train', 'monorail', 'skylift', 'ferry', 'peoplemover', 'cable_car')),
  route_length_meters DECIMAL(10,2),
  stations_count INTEGER,
  vehicle_capacity INTEGER,
  vehicles_count INTEGER,
  round_trip_duration_seconds INTEGER,
  accessibility_features TEXT[],
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(ride_id)
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_ride_water_details_ride_id ON public.ride_water_details(ride_id);
CREATE INDEX IF NOT EXISTS idx_ride_dark_details_ride_id ON public.ride_dark_details(ride_id);
CREATE INDEX IF NOT EXISTS idx_ride_flat_details_ride_id ON public.ride_flat_details(ride_id);
CREATE INDEX IF NOT EXISTS idx_ride_kiddie_details_ride_id ON public.ride_kiddie_details(ride_id);
CREATE INDEX IF NOT EXISTS idx_ride_transportation_details_ride_id ON public.ride_transportation_details(ride_id);

-- Enable RLS
ALTER TABLE public.ride_water_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ride_dark_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ride_flat_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ride_kiddie_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ride_transportation_details ENABLE ROW LEVEL SECURITY;

-- RLS Policies (read-only for all, write for authenticated)
CREATE POLICY "Water ride details are viewable by everyone"
  ON public.ride_water_details FOR SELECT USING (true);

CREATE POLICY "Dark ride details are viewable by everyone"
  ON public.ride_dark_details FOR SELECT USING (true);

CREATE POLICY "Flat ride details are viewable by everyone"
  ON public.ride_flat_details FOR SELECT USING (true);

CREATE POLICY "Kiddie ride details are viewable by everyone"
  ON public.ride_kiddie_details FOR SELECT USING (true);

CREATE POLICY "Transportation ride details are viewable by everyone"
  ON public.ride_transportation_details FOR SELECT USING (true);

-- Write policies (moderators only)
CREATE POLICY "Moderators can manage water ride details"
  ON public.ride_water_details FOR ALL 
  USING (public.is_moderator(auth.uid()));

CREATE POLICY "Moderators can manage dark ride details"
  ON public.ride_dark_details FOR ALL 
  USING (public.is_moderator(auth.uid()));

CREATE POLICY "Moderators can manage flat ride details"
  ON public.ride_flat_details FOR ALL 
  USING (public.is_moderator(auth.uid()));

CREATE POLICY "Moderators can manage kiddie ride details"
  ON public.ride_kiddie_details FOR ALL 
  USING (public.is_moderator(auth.uid()));

CREATE POLICY "Moderators can manage transportation ride details"
  ON public.ride_transportation_details FOR ALL 
  USING (public.is_moderator(auth.uid()));

-- Add update triggers
CREATE TRIGGER update_ride_water_details_updated_at
  BEFORE UPDATE ON public.ride_water_details
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER update_ride_dark_details_updated_at
  BEFORE UPDATE ON public.ride_dark_details
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER update_ride_flat_details_updated_at
  BEFORE UPDATE ON public.ride_flat_details
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER update_ride_kiddie_details_updated_at
  BEFORE UPDATE ON public.ride_kiddie_details
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER update_ride_transportation_details_updated_at
  BEFORE UPDATE ON public.ride_transportation_details
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();