# frozen_string_literal: true

require "date"
require "test_helper"
require "port_repo"
require "db_port"

class FakeDbForPorts < FakeDB
	def initialize(rows=[])
		super({})
		@rows = rows
	end

	def exec_defer(_, _)
		EMPromise.resolve(@rows)
	end
end

class PortRepoDbTest < Minitest::Test
	def setup
		@output = FullManual.new
	end

	def test_list_returns_db_ports
		rows = [
			{
				"id" => "port1",
				"processing_status" => "complete",
				"actual_foc_date" => DateTime.now,
				"updated_at" => DateTime.now,
				"tel" => "+12225551234",
				"customer_id" => "cust1",
				"backend_sgx" => "sgx"
			}
		]
		db = FakeDbForPorts.new(rows)
		repo = PortRepo::Db.new(@output, db: db)

		ports = repo.list.sync

		assert_equal 1, ports.length
		assert_kind_of DbPort, ports[0]
		assert_equal "port1", ports[0].id
		assert_equal "complete", ports[0].processing_status
	end
	em :test_list_returns_db_ports

	def test_list_empty_when_no_rows
		db = FakeDbForPorts.new([])
		repo = PortRepo::Db.new(@output, db: db)

		ports = repo.list.sync

		assert_equal 0, ports.length
	end
	em :test_list_empty_when_no_rows
end

class PortRepoBandwidthTest < Minitest::Test
	def setup
		@output = FullManual.new
	end

	def test_list_returns_empty_when_no_ports
		stub_request(
			:get,
			/https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
		).to_return(status: 204, body: "")

		repo = PortRepo::Bandwidth.new(@output)
		ports = repo.list

		assert_equal 0, ports.length
	end

	def test_list_wraps_ports_in_port_with_backend
		stub_request(
			:get,
			/https:\/\/dashboard\.bandwidth\.com\/v1\.0\/accounts\/.*\/portins/
		).to_return(status: 200, body: <<~XML)
			<LNPResponseWrapper>
				<TotalCount>1</TotalCount>
				<Links/>
				<lnpPortInfoForGivenStatus>
					<OrderId>abc123</OrderId>
					<CustomerOrderId>cust456</CustomerOrderId>
					<BillingTelephoneNumber>5551234567</BillingTelephoneNumber>
					<lastModifiedDate>2025-01-13T12:00:00.000Z</lastModifiedDate>
					<OrderDate>2025-01-12T12:00:00.000Z</OrderDate>
					<ProcessingStatus>COMPLETE</ProcessingStatus>
				</lnpPortInfoForGivenStatus>
			</LNPResponseWrapper>
		XML

		repo = PortRepo::Bandwidth.new(@output)
		ports = repo.list

		assert_equal 1, ports.length
		port = ports[0]
		assert_kind_of PortRepo::Bandwidth::PortWithBackend, port
		assert_equal "abc123", port.id
		assert_equal "cust456", port.customer_id
		assert_equal "5551234567", port.tel
		assert_equal CONFIG[:sgx], port.backend_sgx
	end
end
