import json

from pytest_mock import MockFixture
from src.settings import Settings


class TestPostgresUrl:
    def test_returns_url_from_secret(self, mocker: MockFixture) -> None:
        """postgres_secret_nameが設定されている場合、シークレットから取得したユーザー名とパスワードを使用してURLを返すこと"""
        mock_secret = {"username": "secret_user", "password": "secret_pass"}
        mocker.patch("src.settings.parameters.get_secret", return_value=json.dumps(mock_secret))
        settings = Settings(
            postgres_host="localhost",
            postgres_port=5432,
            postgres_db="postgres",
            postgres_secret_name="my-secret",  # noqa:S106
        )

        expected = "host=localhost port=5432 user=secret_user password=secret_pass dbname=postgres sslmode=disable"
        assert settings.postgres_url == expected

    def test_returns_url_from_env_vars(self) -> None:
        """postgres_secret_nameが設定されていない場合、環境変数から取得したユーザー名とパスワードを使用してURLを返すこと"""
        settings = Settings(
            postgres_host="localhost",
            postgres_port=5432,
            postgres_user="env_user",
            postgres_password="env_pass",  # noqa:S106
            postgres_db="postgres",
        )

        expected = "host=localhost port=5432 user=env_user password=env_pass dbname=postgres"
        assert settings.postgres_url == expected


class TestManagementUserIssuer:
    def test_returns_none_when_pool_id_is_none(self):
        settings = Settings(management_user_pool_id=None)
        assert settings.management_user_issuer is None

    def test_returns_localhost_url_when_stage_is_local(self):
        settings = Settings(stage="local", management_user_pool_id="ap-northeast-1_abc123")
        assert settings.management_user_issuer == "http://localhost:4566/ap-northeast-1_abc123"

    def test_returns_cognito_url_when_stage_is_not_local(self):
        settings = Settings(stage="dev", management_user_pool_id="ap-northeast-1_abc123")
        assert (
            settings.management_user_issuer == "https://cognito-idp.ap-northeast-1.amazonaws.com/ap-northeast-1_abc123"
        )

    def test_returns_cognito_url_when_stage_is_none(self):
        settings = Settings(stage=None, management_user_pool_id="us-east-1_xyz789")
        assert settings.management_user_issuer == "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xyz789"


class TestManagementUserJwksUrl:
    def test_returns_none_when_pool_id_is_none(self):
        settings = Settings(management_user_pool_id=None)
        assert settings.management_user_jwks_url is None

    def test_returns_floci_url_when_stage_is_local(self):
        settings = Settings(stage="local", management_user_pool_id="ap-northeast-1_abc123")
        assert settings.management_user_jwks_url == "http://floci:4566/ap-northeast-1_abc123/.well-known/jwks.json"

    def test_returns_cognito_jwks_url_when_stage_is_not_local(self):
        settings = Settings(stage="dev", management_user_pool_id="ap-northeast-1_abc123")
        assert (
            settings.management_user_jwks_url
            == "https://cognito-idp.ap-northeast-1.amazonaws.com/ap-northeast-1_abc123/.well-known/jwks.json"
        )

    def test_returns_cognito_jwks_url_when_stage_is_none(self):
        settings = Settings(stage=None, management_user_pool_id="us-east-1_xyz789")
        assert (
            settings.management_user_jwks_url
            == "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xyz789/.well-known/jwks.json"
        )
