-- Backfill initial version records for existing entities
-- This populates the Recent Changes tab with current data

-- 1. Backfill Park Versions (16 parks)
INSERT INTO park_versions (
  park_id,
  version_number,
  created_by,
  change_type,
  change_reason,
  is_current,
  name,
  slug,
  description,
  park_type,
  status,
  location_id,
  operator_id,
  property_owner_id,
  opening_date,
  closing_date,
  opening_date_precision,
  closing_date_precision,
  website_url,
  phone,
  email,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id
)
SELECT 
  id,
  1,
  NULL,
  'created',
  'Initial version created during database backfill',
  true,
  name,
  slug,
  description,
  park_type,
  status,
  location_id,
  operator_id,
  property_owner_id,
  opening_date,
  closing_date,
  opening_date_precision,
  closing_date_precision,
  website_url,
  phone,
  email,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id
FROM parks
WHERE NOT EXISTS (
  SELECT 1 FROM park_versions WHERE park_versions.park_id = parks.id
);

-- 2. Backfill Ride Versions (20 rides) - with column mapping
INSERT INTO ride_versions (
  ride_id,
  version_number,
  created_by,
  change_type,
  change_reason,
  is_current,
  name,
  slug,
  description,
  category,
  status,
  park_id,
  manufacturer_id,
  designer_id,
  ride_model_id,
  opening_date,
  closing_date,
  opening_date_precision,
  closing_date_precision,
  height_requirement_cm,
  max_speed_kmh,
  duration_seconds,
  capacity_per_hour,
  gforce_max,
  inversions_count,
  length_meters,
  height_meters,
  drop_meters,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id,
  track_material
)
SELECT 
  id,
  1,
  NULL,
  'created',
  'Initial version created during database backfill',
  true,
  name,
  slug,
  description,
  category,
  status,
  park_id,
  manufacturer_id,
  designer_id,
  ride_model_id,
  opening_date,
  closing_date,
  opening_date_precision,
  closing_date_precision,
  height_requirement,
  max_speed_kmh,
  duration_seconds,
  capacity_per_hour,
  max_g_force,
  inversions,
  length_meters,
  max_height_meters,
  drop_height_meters,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id,
  track_material
FROM rides
WHERE NOT EXISTS (
  SELECT 1 FROM ride_versions WHERE ride_versions.ride_id = rides.id
);

-- 3. Backfill Company Versions (12 companies)
INSERT INTO company_versions (
  company_id,
  version_number,
  created_by,
  change_type,
  change_reason,
  is_current,
  name,
  slug,
  description,
  company_type,
  person_type,
  founded_year,
  founded_date,
  founded_date_precision,
  headquarters_location,
  website_url,
  logo_url,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id
)
SELECT 
  id,
  1,
  NULL,
  'created',
  'Initial version created during database backfill',
  true,
  name,
  slug,
  description,
  company_type,
  person_type,
  founded_year,
  founded_date,
  founded_date_precision,
  headquarters_location,
  website_url,
  logo_url,
  banner_image_url,
  banner_image_id,
  card_image_url,
  card_image_id
FROM companies
WHERE NOT EXISTS (
  SELECT 1 FROM company_versions WHERE company_versions.company_id = companies.id
);