# frozen_string_literal: true

class CreateRules < ActiveRecord::Migration[8.1]
  def change
    create_table :rules, force: :cascade do |t|
      # Rule classification
      t.string :rule_type, null: false, index: true
      t.string :action, null: false, index: true
      t.string :source, limit: 100, default: 'manual', index: true

      # Priority for rule evaluation (higher = more specific)
      t.integer :priority, index: true

      # Rule conditions (JSON for flexibility)
      t.json :conditions, default: {}

      # Rule metadata (JSON for extensibility)
      t.json :metadata, default: {}

      # Rule lifecycle
      t.boolean :enabled, default: true, null: false, index: true
      t.datetime :expires_at, index: true

      # Relationships
      t.references :user, foreign_key: true
      t.references :network_range, foreign_key: true

      t.timestamps

      # Composite indexes for common queries
      t.index [:rule_type, :enabled], name: 'idx_rules_type_enabled'
      t.index [:enabled, :expires_at], name: 'idx_rules_active'
      t.index [:updated_at, :id], name: 'idx_rules_sync'
    end
  end
end