class CreateWafPolicies < ActiveRecord::Migration[8.1]
  def change
    create_table :waf_policies do |t|
      t.string :name, null: false
      t.text :description
      t.string :policy_type, null: false, default: 'country'
      t.string :action, null: false, default: 'deny'
      t.json :targets, default: []
      t.boolean :enabled, default: true, null: false
      t.datetime :expires_at
      t.references :user, null: false, foreign_key: true
      t.json :additional_data, default: {}

      t.timestamps
    end

    # Add indexes for efficient policy matching
    add_index :waf_policies, [:policy_type, :enabled], name: "idx_waf_policies_type_enabled"
    add_index :waf_policies, :enabled
    add_index :waf_policies, :expires_at
    add_index :waf_policies, :name, unique: true
  end
end
