
-- ============================================================================
-- COMPREHENSIVE VERSIONING FIX: Add missing fields to ride_versions
-- ============================================================================

-- Add missing columns from rides table to ride_versions
ALTER TABLE public.ride_versions
  ADD COLUMN IF NOT EXISTS ride_sub_type TEXT,
  ADD COLUMN IF NOT EXISTS age_requirement INTEGER,
  ADD COLUMN IF NOT EXISTS coaster_type TEXT,
  ADD COLUMN IF NOT EXISTS seating_type TEXT,
  ADD COLUMN IF NOT EXISTS intensity_level TEXT,
  ADD COLUMN IF NOT EXISTS image_url TEXT;

-- Add comments for new columns
COMMENT ON COLUMN public.ride_versions.ride_sub_type IS 'Subtype classification within the main category';
COMMENT ON COLUMN public.ride_versions.age_requirement IS 'Minimum age requirement in years';
COMMENT ON COLUMN public.ride_versions.coaster_type IS 'Type of roller coaster (e.g., wooden, steel, hybrid)';
COMMENT ON COLUMN public.ride_versions.seating_type IS 'Type of seating configuration';
COMMENT ON COLUMN public.ride_versions.intensity_level IS 'Intensity level rating (e.g., mild, moderate, extreme)';
COMMENT ON COLUMN public.ride_versions.image_url IS 'Primary image URL for the ride';

-- ============================================================================
-- Create ride_former_names relational table (replacing JSONB)
-- ============================================================================

CREATE TABLE IF NOT EXISTS public.ride_former_names (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  ride_id UUID NOT NULL REFERENCES public.rides(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  used_from DATE,
  used_until DATE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);

-- Add indexes for performance
CREATE INDEX IF NOT EXISTS idx_ride_former_names_ride_id ON public.ride_former_names(ride_id);
CREATE INDEX IF NOT EXISTS idx_ride_former_names_dates ON public.ride_former_names(used_from, used_until);

-- Enable RLS
ALTER TABLE public.ride_former_names ENABLE ROW LEVEL SECURITY;

-- Create RLS policies
CREATE POLICY "Public can view former names"
  ON public.ride_former_names
  FOR SELECT
  USING (true);

CREATE POLICY "Moderators can manage former names"
  ON public.ride_former_names
  FOR ALL
  USING (is_moderator(auth.uid()) AND has_aal2());

-- Add comments
COMMENT ON TABLE public.ride_former_names IS 'Historical names for rides - relational replacement for JSONB former_names';
COMMENT ON COLUMN public.ride_former_names.name IS 'Former name of the ride';
COMMENT ON COLUMN public.ride_former_names.used_from IS 'Date when this name started being used';
COMMENT ON COLUMN public.ride_former_names.used_until IS 'Date when this name stopped being used';

-- ============================================================================
-- Remove former_names JSONB column from ride_versions (violates no-JSONB rule)
-- ============================================================================

ALTER TABLE public.ride_versions DROP COLUMN IF EXISTS former_names;
