"""
Drive Service HTTP Client 單元測試 (6.3)
使用 mock 測試，不需要實際 Drive Service 上線
"""
import pytest
from unittest.mock import patch, MagicMock
from app.services.drive_service import DriveServiceClient, get_drive_quota_by_job_level


class TestGetDriveQuotaByJobLevel:
    """職級配額對應測試"""

    def test_junior_quota(self):
        assert get_drive_quota_by_job_level("Junior") == 50

    def test_mid_quota(self):
        assert get_drive_quota_by_job_level("Mid") == 100

    def test_senior_quota(self):
        assert get_drive_quota_by_job_level("Senior") == 200

    def test_manager_quota(self):
        assert get_drive_quota_by_job_level("Manager") == 500

    def test_unknown_level_defaults_to_junior(self):
        assert get_drive_quota_by_job_level("Unknown") == 50

    def test_none_defaults_to_junior(self):
        assert get_drive_quota_by_job_level(None) == 50


class TestDriveServiceClient:
    """Drive Service HTTP Client 測試"""

    def setup_method(self):
        self.client = DriveServiceClient(
            base_url="https://drive-api.ease.taipei",
            timeout=5,
        )

    def test_create_user_service_unavailable(self):
        """Drive Service 未上線時回傳 warning 結果"""
        from requests.exceptions import ConnectionError as RequestsConnectionError
        with patch.object(self.client.session, "post") as mock_post:
            mock_post.side_effect = RequestsConnectionError("Connection refused")
            result = self.client.create_user(
                tenant_id=1,
                keycloak_user_id="kc-uuid-123",
                username="test.user",
                email="test.user@porscheworld.tw",
                display_name="Test User",
                quota_gb=100,
            )
        assert result["created"] is False
        assert result["error"] is not None
        assert "test.user" in str(result)

    def test_create_user_success(self):
        """Drive Service 正常回應時成功建立"""
        with patch.object(self.client.session, "post") as mock_post:
            mock_response = MagicMock()
            mock_response.status_code = 201
            mock_response.json.return_value = {
                "id": 42,
                "username": "test.user",
                "quota_gb": 100,
                "drive_url": "https://drive.ease.taipei/test.user",
            }
            mock_post.return_value = mock_response

            result = self.client.create_user(
                tenant_id=1,
                keycloak_user_id="kc-uuid-123",
                username="test.user",
                email="test.user@porscheworld.tw",
                display_name="Test User",
                quota_gb=100,
            )
        assert result["created"] is True
        assert result["error"] is None
        assert result["quota_gb"] == 100

    def test_get_quota_service_unavailable(self):
        """Drive Service 未上線時查詢配額回傳 None"""
        from requests.exceptions import ConnectionError as RequestsConnectionError
        with patch.object(self.client.session, "get") as mock_get:
            mock_get.side_effect = RequestsConnectionError("Connection refused")
            result = self.client.get_quota(drive_user_id=42)
        assert result is None

    def test_disable_user_service_unavailable(self):
        """Drive Service 未上線時停用帳號回傳 False"""
        from requests.exceptions import ConnectionError as RequestsConnectionError
        with patch.object(self.client.session, "delete") as mock_delete:
            mock_delete.side_effect = RequestsConnectionError("Connection refused")
            result = self.client.disable_user(drive_user_id=42)
        assert result["disabled"] is False
        assert result["error"] is not None
