class AddBillingAndRecordings < ActiveRecord::Migration[7.2]
  def change
    create_table :plans, id: :uuid do |t|
      t.string :slug, null: false
      t.string :name, null: false
      t.string :stripe_price_id
      t.jsonb :features, null: false, default: {}
      t.timestamps
    end
    add_index :plans, :slug, unique: true

    create_table :subscriptions, id: :uuid do |t|
      t.references :team, null: false, type: :uuid, foreign_key: true, index: { unique: true }
      t.references :plan, null: false, type: :uuid, foreign_key: true
      t.string :status, null: false, default: "active"
      t.string :stripe_customer_id
      t.string :stripe_subscription_id
      t.datetime :current_period_start
      t.datetime :current_period_end
      t.boolean :cancel_at_period_end, default: false, null: false
      t.timestamps
    end
    add_index :subscriptions, :stripe_subscription_id, unique: true, where: "stripe_subscription_id IS NOT NULL"

    create_table :recordings, id: :uuid do |t|
      t.references :stream_session, null: false, type: :uuid, foreign_key: true, index: { unique: true }
      t.references :team, null: false, type: :uuid, foreign_key: true
      t.string :status, null: false, default: "processing"
      t.string :storage_path
      t.datetime :expires_at
      t.timestamps
    end
    add_index :recordings, :expires_at

    create_table :team_invitations, id: :uuid do |t|
      t.references :team, null: false, type: :uuid, foreign_key: true
      t.string :email, null: false
      t.string :token_digest, null: false
      t.string :role, null: false, default: "member"
      t.datetime :expires_at, null: false
      t.datetime :accepted_at
      t.timestamps
    end
    add_index :team_invitations, [:team_id, :email]
    add_index :team_invitations, :token_digest, unique: true
  end
end
