-- Add foreign key constraints to submission_items table
ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_park_submission
  FOREIGN KEY (park_submission_id) 
  REFERENCES park_submissions(id)
  ON DELETE CASCADE;

ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_ride_submission
  FOREIGN KEY (ride_submission_id) 
  REFERENCES ride_submissions(id)
  ON DELETE CASCADE;

ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_company_submission
  FOREIGN KEY (company_submission_id) 
  REFERENCES company_submissions(id)
  ON DELETE CASCADE;

ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_ride_model_submission
  FOREIGN KEY (ride_model_submission_id) 
  REFERENCES ride_model_submissions(id)
  ON DELETE CASCADE;

ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_photo_submission
  FOREIGN KEY (photo_submission_id) 
  REFERENCES photo_submissions(id)
  ON DELETE CASCADE;

ALTER TABLE submission_items
  ADD CONSTRAINT fk_submission_items_timeline_event_submission
  FOREIGN KEY (timeline_event_submission_id) 
  REFERENCES timeline_event_submissions(id)
  ON DELETE CASCADE;

-- Add indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_submission_items_park_submission_id 
  ON submission_items(park_submission_id);
CREATE INDEX IF NOT EXISTS idx_submission_items_ride_submission_id 
  ON submission_items(ride_submission_id);
CREATE INDEX IF NOT EXISTS idx_submission_items_company_submission_id 
  ON submission_items(company_submission_id);
CREATE INDEX IF NOT EXISTS idx_submission_items_ride_model_submission_id 
  ON submission_items(ride_model_submission_id);
CREATE INDEX IF NOT EXISTS idx_submission_items_photo_submission_id 
  ON submission_items(photo_submission_id);
CREATE INDEX IF NOT EXISTS idx_submission_items_timeline_event_submission_id 
  ON submission_items(timeline_event_submission_id);