# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 2025_12_02_070000) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "pg_catalog.plpgsql"

  create_table "active_storage_attachments", force: :cascade do |t|
    t.bigint "blob_id", null: false
    t.datetime "created_at", null: false
    t.string "name", null: false
    t.bigint "record_id", null: false
    t.string "record_type", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.bigint "byte_size", null: false
    t.string "checksum"
    t.string "content_type"
    t.datetime "created_at", null: false
    t.string "filename", null: false
    t.string "key", null: false
    t.text "metadata"
    t.string "service_name", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "active_storage_variant_records", force: :cascade do |t|
    t.bigint "blob_id", null: false
    t.string "variation_digest", null: false
    t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
  end

  create_table "data_imports", force: :cascade do |t|
    t.datetime "completed_at"
    t.datetime "created_at", null: false
    t.text "error_message"
    t.integer "failed_records", default: 0
    t.string "filename", null: false
    t.json "import_stats", default: {}, comment: "Detailed import statistics"
    t.string "import_type", null: false, comment: "ASN or Country import"
    t.integer "processed_records", default: 0
    t.datetime "started_at"
    t.string "status", default: "pending", null: false, comment: "pending, processing, completed, failed"
    t.integer "total_records", default: 0
    t.datetime "updated_at", null: false
    t.index ["created_at"], name: "index_data_imports_on_created_at"
    t.index ["import_type"], name: "index_data_imports_on_import_type"
    t.index ["status"], name: "index_data_imports_on_status"
  end

  create_table "dsns", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.boolean "enabled", default: true, null: false
    t.string "key"
    t.string "name"
    t.datetime "updated_at", null: false
    t.index ["key"], name: "index_dsns_on_key", unique: true
  end

  create_table "events", force: :cascade do |t|
    t.string "agent_name"
    t.string "agent_version"
    t.integer "asn"
    t.string "asn_org"
    t.text "blocked_reason"
    t.string "company"
    t.string "country"
    t.datetime "created_at", null: false
    t.string "environment"
    t.inet "ip_address"
    t.boolean "is_bot", default: false, null: false
    t.boolean "is_datacenter", default: false, null: false
    t.boolean "is_proxy", default: false, null: false
    t.boolean "is_vpn", default: false, null: false
    t.bigint "network_range_id"
    t.json "payload"
    t.bigint "request_host_id"
    t.string "request_id", null: false
    t.integer "request_method", default: 0
    t.string "request_path"
    t.string "request_protocol"
    t.string "request_segment_ids"
    t.string "request_url"
    t.integer "response_status"
    t.integer "response_time_ms"
    t.bigint "rule_id"
    t.string "server_name"
    t.jsonb "tags", default: [], null: false
    t.datetime "timestamp", null: false
    t.datetime "updated_at", null: false
    t.string "user_agent"
    t.integer "waf_action", default: 0, null: false
    t.index ["asn"], name: "index_events_on_asn"
    t.index ["company"], name: "index_events_on_company"
    t.index ["country"], name: "index_events_on_country"
    t.index ["ip_address"], name: "index_events_on_ip_address"
    t.index ["is_bot"], name: "index_events_on_is_bot"
    t.index ["is_datacenter", "is_vpn", "is_proxy"], name: "index_events_on_network_flags"
    t.index ["network_range_id"], name: "index_events_on_network_range_id"
    t.index ["request_host_id", "request_method", "request_segment_ids"], name: "idx_events_host_method_path"
    t.index ["request_host_id"], name: "index_events_on_request_host_id"
    t.index ["request_id"], name: "index_events_on_request_id", unique: true
    t.index ["request_segment_ids"], name: "index_events_on_request_segment_ids"
    t.index ["rule_id"], name: "index_events_on_rule_id"
    t.index ["tags"], name: "index_events_on_tags", using: :gin
    t.index ["timestamp"], name: "index_events_on_timestamp"
    t.index ["waf_action"], name: "index_events_on_waf_action"
  end

  create_table "network_ranges", force: :cascade do |t|
    t.text "abuser_scores"
    t.text "additional_data"
    t.integer "asn"
    t.string "asn_org"
    t.string "company"
    t.string "country"
    t.datetime "created_at", null: false
    t.text "creation_reason"
    t.boolean "is_datacenter", default: false
    t.boolean "is_proxy", default: false
    t.boolean "is_vpn", default: false
    t.datetime "last_api_fetch"
    t.inet "network", null: false
    t.jsonb "network_data", default: {}
    t.datetime "policies_evaluated_at"
    t.string "source", default: "api_imported", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["asn"], name: "index_network_ranges_on_asn"
    t.index ["asn_org"], name: "index_network_ranges_on_asn_org"
    t.index ["company"], name: "index_network_ranges_on_company"
    t.index ["country"], name: "index_network_ranges_on_country"
    t.index ["is_datacenter", "is_proxy", "is_vpn"], name: "idx_network_flags"
    t.index ["is_datacenter"], name: "index_network_ranges_on_is_datacenter"
    t.index ["network"], name: "index_network_ranges_on_network", opclass: :inet_ops, using: :gist
    t.index ["network"], name: "index_network_ranges_on_network_unique", unique: true
    t.index ["network_data"], name: "index_network_ranges_on_network_data", using: :gin
    t.index ["policies_evaluated_at"], name: "index_network_ranges_on_policies_evaluated_at"
    t.index ["source"], name: "index_network_ranges_on_source"
    t.index ["user_id"], name: "index_network_ranges_on_user_id"
  end

  create_table "path_segments", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "first_seen_at", null: false
    t.string "segment", null: false
    t.datetime "updated_at", null: false
    t.integer "usage_count", default: 1, null: false
    t.index ["segment"], name: "index_path_segments_on_segment", unique: true
  end

  create_table "request_actions", force: :cascade do |t|
    t.string "action", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["action"], name: "index_request_actions_on_action", unique: true
  end

  create_table "request_hosts", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "first_seen_at", null: false
    t.string "hostname", null: false
    t.datetime "updated_at", null: false
    t.integer "usage_count", default: 1, null: false
    t.index ["hostname"], name: "index_request_hosts_on_hostname", unique: true
  end

  create_table "request_methods", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.string "method", null: false
    t.datetime "updated_at", null: false
    t.index ["method"], name: "index_request_methods_on_method", unique: true
  end

  create_table "request_protocols", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.string "protocol", null: false
    t.datetime "updated_at", null: false
    t.index ["protocol"], name: "index_request_protocols_on_protocol", unique: true
  end

  create_table "rules", force: :cascade do |t|
    t.json "conditions", default: {}
    t.datetime "created_at", null: false
    t.boolean "enabled", default: true, null: false
    t.datetime "expires_at"
    t.json "metadata", default: {}
    t.bigint "network_range_id"
    t.integer "priority"
    t.string "source", limit: 100, default: "manual"
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.integer "waf_action", default: 0, null: false
    t.bigint "waf_policy_id"
    t.integer "waf_rule_type", default: 0, null: false
    t.index ["enabled", "expires_at"], name: "idx_rules_active"
    t.index ["enabled"], name: "index_rules_on_enabled"
    t.index ["expires_at"], name: "index_rules_on_expires_at"
    t.index ["network_range_id", "waf_action", "waf_policy_id", "expires_at"], name: "index_rules_on_network_policy_expires_unique", unique: true, where: "(((source)::text = 'policy'::text) AND (expires_at IS NOT NULL))"
    t.index ["network_range_id", "waf_action", "waf_policy_id"], name: "index_rules_on_network_policy_unique", unique: true, where: "(((source)::text = 'policy'::text) AND (expires_at IS NULL))"
    t.index ["network_range_id"], name: "index_rules_on_network_range_id"
    t.index ["priority"], name: "index_rules_on_priority"
    t.index ["source", "expires_at"], name: "index_rules_on_source_expires"
    t.index ["source"], name: "index_rules_on_source"
    t.index ["updated_at", "id"], name: "idx_rules_sync"
    t.index ["user_id"], name: "index_rules_on_user_id"
    t.index ["waf_action"], name: "index_rules_on_waf_action"
    t.index ["waf_policy_id", "expires_at"], name: "index_rules_on_policy_expires"
    t.index ["waf_policy_id"], name: "idx_rules_waf_policy"
    t.index ["waf_policy_id"], name: "index_rules_on_waf_policy_id"
    t.index ["waf_rule_type"], name: "index_rules_on_waf_rule_type"
  end

  create_table "sessions", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.inet "ip_address"
    t.datetime "updated_at", null: false
    t.string "user_agent"
    t.bigint "user_id", null: false
    t.index ["user_id"], name: "index_sessions_on_user_id"
  end

  create_table "settings", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.string "key"
    t.datetime "updated_at", null: false
    t.string "value"
    t.index ["key"], name: "index_settings_on_key", unique: true
  end

  create_table "users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.string "email_address", null: false
    t.string "password_digest", null: false
    t.integer "role", default: 1, null: false
    t.datetime "updated_at", null: false
    t.index ["email_address"], name: "index_users_on_email_address", unique: true
  end

  create_table "waf_policies", force: :cascade do |t|
    t.json "additional_data", default: {}
    t.datetime "created_at", null: false
    t.text "description"
    t.boolean "enabled", default: true, null: false
    t.datetime "expires_at"
    t.string "name", null: false
    t.string "policy_action", default: "deny", null: false
    t.string "policy_type", default: "country", null: false
    t.json "targets", default: []
    t.datetime "updated_at", null: false
    t.bigint "user_id", null: false
    t.index ["enabled"], name: "index_waf_policies_on_enabled"
    t.index ["expires_at"], name: "index_waf_policies_on_expires_at"
    t.index ["name"], name: "index_waf_policies_on_name", unique: true
    t.index ["policy_type", "enabled"], name: "idx_waf_policies_type_enabled"
    t.index ["user_id"], name: "index_waf_policies_on_user_id"
  end

  add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
  add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
  add_foreign_key "events", "request_hosts"
  add_foreign_key "events", "rules"
  add_foreign_key "network_ranges", "users"
  add_foreign_key "rules", "network_ranges"
  add_foreign_key "rules", "users"
  add_foreign_key "rules", "waf_policies"
  add_foreign_key "sessions", "users"
  add_foreign_key "waf_policies", "users"
end
