use "std/pg/uri"

fn pg_uri_test_value(assignments: TextList, name: Text) -> Text:
    var index: Number = 0
    while index + 1 < list_count(assignments):
        if list_get(assignments, index) == name:
            return list_get(assignments, index + 1)
        set index = index + 2
    return ""

start:
    let parsed: Result[TextList, Text] = pg_uri_parse_config("postgresql://yaza%2Duser:p%40ss@127.0.0.1:5433/catalog?sslmode=disable&sslrootcert=%2Ftmp%2Fyaza-root.crt&connect_timeout=9&application_name=YAZA%20API")
    assert result_is_ok(parsed)
    let assignments: TextList = result_unwrap(parsed)
    assert pg_uri_test_value(assignments, "PGUSER") == "yaza-user"
    assert pg_uri_test_value(assignments, "PGPASSWORD") == "p@ss"
    assert pg_uri_test_value(assignments, "PGHOST") == "127.0.0.1"
    assert pg_uri_test_value(assignments, "PGPORT") == "5433"
    assert pg_uri_test_value(assignments, "PGDATABASE") == "catalog"
    assert pg_uri_test_value(assignments, "PGSSLMODE") == "disable"
    assert pg_uri_test_value(assignments, "PGSSLROOTCERT") == "/tmp/yaza-root.crt"
    assert pg_uri_test_value(assignments, "PGCONNECT_TIMEOUT") == "9"
    assert pg_uri_test_value(assignments, "PGAPPNAME") == "YAZA API"

    let ipv6: Result[TextList, Text] = pg_uri_parse_config("postgres://user@[::1]:5432/local_db?sslmode=disable")
    assert result_is_ok(ipv6)
    assert pg_uri_test_value(result_unwrap(ipv6), "PGHOST") == "::1"
    assert pg_uri_test_value(result_unwrap(ipv6), "PGPORT") == "5432"

    let socket_default: Result[TextList, Text] = pg_uri_parse_config("postgresql:///local_db")
    assert result_is_ok(socket_default)
    assert pg_uri_test_value(result_unwrap(socket_default), "PGHOST") == ""
    assert pg_uri_test_value(result_unwrap(socket_default), "PGDATABASE") == "local_db"

    let unix_socket: Result[TextList, Text] = pg_uri_parse_config("postgresql://local_user@%2Fvar%2Frun%2Fpostgresql/local_db")
    assert result_is_ok(unix_socket)
    assert pg_uri_test_value(result_unwrap(unix_socket), "PGHOST") == "/var/run/postgresql"

    assert not result_is_ok(pg_uri_parse_config("mysql://user@localhost/db"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/db%2Fother"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost:0/db"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost:65536/db"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost:/db"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/db?unknown=value"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/db?sslmode=disable&"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/db?sslmode=%00"))
    assert not result_is_ok(pg_uri_parse_config("postgresql://user@localhost/db#fragment"))

    assert pg_uri_configure_environment("postgresql://local_user:local_pass@127.0.0.1:5432/local_db?sslmode=disable&sslrootcert=%2Ftmp%2Ftrusted-root.crt") == 0
    assert env_get("PGUSER") == "local_user"
    assert env_get("PGPASSWORD") == "local_pass"
    assert env_get("PGHOST") == "127.0.0.1"
    assert env_get("PGDATABASE") == "local_db"
    assert env_get("PGSSLMODE") == "disable"
    assert env_get("PGSSLROOTCERT") == "/tmp/trusted-root.crt"
    assert pg_uri_transport_policy_valid("127.0.0.1", "disable")
    assert pg_uri_transport_policy_valid("::1", "disable")
    assert not pg_uri_transport_policy_valid("localhost", "disable")
    assert not pg_uri_transport_policy_valid("db.example.test", "disable")
    assert pg_uri_transport_policy_valid("db.example.test", "verify-full")
    assert pg_uri_transport_policy_valid("127.0.0.1", "verify-full")
    assert not pg_uri_transport_policy_valid("db.example.test", "prefer")
    assert not pg_uri_transport_policy_valid("db.example.test", "allow")
    assert not pg_uri_transport_policy_valid("db.example.test", "require")
    assert not pg_uri_transport_policy_valid("db.example.test", "verify-ca")
    assert not pg_uri_transport_policy_valid("db.example.test", "")
