class CreateTheatreSkipVotesAndEvents < ActiveRecord::Migration[8.0]
  def change
    create_table :theatre_skip_votes, primary_key: [:theatre_id, :post_id, :user_id] do |t|
      t.references :theatre, null: false, foreign_key: true
      t.references :post, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end

    create_table :theatre_skip_events do |t|
      t.references :theatre, null: false, foreign_key: true
      t.references :post, null: false, foreign_key: true
      t.references :skipped_by_user, null: false, foreign_key: { to_table: :users }
      t.integer :programme_position
      t.datetime :created_at, null: false
    end

    create_table :theatre_skip_event_voters, primary_key: [:theatre_skip_event_id, :user_id] do |t|
      t.references :theatre_skip_event, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
    end

    create_table :theatre_skip_event_tags, primary_key: [:theatre_skip_event_id, :tag_id] do |t|
      t.references :theatre_skip_event, null: false, foreign_key: true
      t.references :tag, null: false, foreign_key: true
    end

    add_index :theatre_skip_events, [:theatre_id, :created_at]
    add_index :theatre_skip_votes, [:theatre_id, :post_id, :created_at],
              name: 'idx_theatre_skip_votes_theatre_post_created'
    add_index :theatre_skip_event_voters, [:user_id, :theatre_skip_event_id],
              name: 'idx_theatre_skip_event_voters_user_event'
    add_index :theatre_skip_event_tags, [:tag_id, :theatre_skip_event_id],
              name: 'idx_theatre_skip_event_tags_tag_event'
  end
end
