# frozen_string_literal: true

require "test_helper"

class Ipv4RangeTest < ActiveSupport::TestCase
  test "creates range from CIDR notation" do
    range = Ipv4Range.create!(cidr: "192.168.1.0/24")

    assert_equal 24, range.network_prefix
    assert range.network_start.present?
    assert range.network_end.present?
    assert range.network_start < range.network_end
  end

  test "calculates correct range for /32 single IP" do
    range = Ipv4Range.create!(cidr: "192.168.1.100/32")

    assert_equal 32, range.network_prefix
    assert_equal range.network_start, range.network_end
  end

  test "calculates correct range for /8 large network" do
    range = Ipv4Range.create!(cidr: "10.0.0.0/8")

    assert_equal 8, range.network_prefix

    # 10.0.0.0 to 10.255.255.255
    ip_start = IPAddr.new("10.0.0.0").to_i
    ip_end = IPAddr.new("10.255.255.255").to_i

    assert_equal ip_start, range.network_start
    assert_equal ip_end, range.network_end
  end

  test "validates network_prefix range" do
    range = Ipv4Range.new(cidr: "192.168.1.0/24")
    range.network_prefix = 33

    assert_not range.valid?
    assert_includes range.errors[:network_prefix], "must be less than or equal to 32"
  end

  test "contains_ip? returns true for IP in range" do
    range = Ipv4Range.create!(cidr: "192.168.1.0/24")

    assert range.contains_ip?("192.168.1.1")
    assert range.contains_ip?("192.168.1.100")
    assert range.contains_ip?("192.168.1.255")
  end

  test "contains_ip? returns false for IP outside range" do
    range = Ipv4Range.create!(cidr: "192.168.1.0/24")

    assert_not range.contains_ip?("192.168.2.1")
    assert_not range.contains_ip?("10.0.0.1")
  end

  test "contains_ip class method finds matching ranges" do
    range1 = Ipv4Range.create!(cidr: "10.0.0.0/8")
    range2 = Ipv4Range.create!(cidr: "192.168.1.0/24")

    results = Ipv4Range.contains_ip("10.5.10.50")

    assert_includes results, range1
    assert_not_includes results, range2
  end

  test "contains_ip returns most specific range first" do
    broad_range = Ipv4Range.create!(cidr: "10.0.0.0/8")
    specific_range = Ipv4Range.create!(cidr: "10.0.1.0/24")

    results = Ipv4Range.contains_ip("10.0.1.50")

    assert_equal specific_range, results.first
  end

  test "to_cidr returns CIDR notation" do
    range = Ipv4Range.create!(cidr: "192.168.1.0/24")

    assert_equal "192.168.1.0/24", range.to_cidr
  end

  test "datacenter scope returns datacenter IPs" do
    datacenter = Ipv4Range.create!(cidr: "1.2.3.0/24", is_datacenter: true)
    regular = Ipv4Range.create!(cidr: "192.168.1.0/24", is_datacenter: false)

    results = Ipv4Range.datacenter

    assert_includes results, datacenter
    assert_not_includes results, regular
  end

  test "stores and retrieves JSON metadata" do
    range = Ipv4Range.create!(cidr: "1.2.3.0/24")
    range.abuser_scores_hash = { "spam" => 0.8, "malware" => 0.3 }
    range.save!

    range.reload
    scores = range.abuser_scores_hash

    assert_equal 0.8, scores["spam"]
    assert_equal 0.3, scores["malware"]
  end

  test "stores IP intelligence metadata" do
    range = Ipv4Range.create!(
      cidr: "1.2.3.0/24",
      company: "Example Corp",
      asn: 12345,
      asn_org: "AS Example",
      is_datacenter: true,
      is_proxy: false,
      is_vpn: false,
      ip_api_country: "US"
    )

    assert_equal "Example Corp", range.company
    assert_equal 12345, range.asn
    assert range.is_datacenter?
    assert_not range.is_proxy?
  end
end
