-- Install missing versioning triggers for automatic version creation
-- These triggers should have been created previously but are missing from the database

-- Clean up any existing triggers first
DROP TRIGGER IF EXISTS create_park_version_on_change ON public.parks;
DROP TRIGGER IF EXISTS create_ride_version_on_change ON public.rides;
DROP TRIGGER IF EXISTS create_company_version_on_change ON public.companies;
DROP TRIGGER IF EXISTS create_ride_model_version_on_change ON public.ride_models;

-- Install versioning trigger for parks
-- Automatically creates a version record whenever a park is inserted or updated
CREATE TRIGGER create_park_version_on_change
  AFTER INSERT OR UPDATE ON public.parks
  FOR EACH ROW
  EXECUTE FUNCTION public.create_relational_version();

-- Install versioning trigger for rides
-- Automatically creates a version record whenever a ride is inserted or updated
CREATE TRIGGER create_ride_version_on_change
  AFTER INSERT OR UPDATE ON public.rides
  FOR EACH ROW
  EXECUTE FUNCTION public.create_relational_version();

-- Install versioning trigger for companies
-- Automatically creates a version record whenever a company is inserted or updated
CREATE TRIGGER create_company_version_on_change
  AFTER INSERT OR UPDATE ON public.companies
  FOR EACH ROW
  EXECUTE FUNCTION public.create_relational_version();

-- Install versioning trigger for ride models
-- Automatically creates a version record whenever a ride model is inserted or updated
CREATE TRIGGER create_ride_model_version_on_change
  AFTER INSERT OR UPDATE ON public.ride_models
  FOR EACH ROW
  EXECUTE FUNCTION public.create_relational_version();