from unittest import mock

from prowler.providers.gcp.models import GCPProject
from prowler.providers.gcp.services.serviceusage.serviceusage_service import Service
from tests.providers.gcp.gcp_fixtures import GCP_PROJECT_ID, set_mocked_gcp_provider


class Test_gemini_api_disabled:
    def test_gemini_api_disabled(self):
        serviceusage_client = mock.MagicMock()
        serviceusage_client.active_services = {
            GCP_PROJECT_ID: [
                Service(
                    name="storage.googleapis.com",
                    title="Cloud Storage API",
                    project_id=GCP_PROJECT_ID,
                )
            ]
        }
        serviceusage_client.project_ids = [GCP_PROJECT_ID]
        serviceusage_client.region = "global"
        serviceusage_client.projects = {
            GCP_PROJECT_ID: GCPProject(
                id=GCP_PROJECT_ID,
                number="123456789012",
                name="test",
                labels={},
                lifecycle_state="ACTIVE",
            )
        }

        with (
            mock.patch(
                "prowler.providers.common.provider.Provider.get_global_provider",
                return_value=set_mocked_gcp_provider(),
            ),
            mock.patch(
                "prowler.providers.gcp.services.gemini.gemini_api_disabled.gemini_api_disabled.serviceusage_client",
                new=serviceusage_client,
            ),
        ):
            from prowler.providers.gcp.services.gemini.gemini_api_disabled.gemini_api_disabled import (
                gemini_api_disabled,
            )

            check = gemini_api_disabled()
            result = check.execute()

            assert len(result) == 1
            assert result[0].status == "PASS"
            assert (
                result[0].status_extended
                == f"Gemini (Generative Language) API is disabled for project {GCP_PROJECT_ID}"
            )
            assert result[0].resource_id == "generativelanguage.googleapis.com"
            assert result[0].project_id == GCP_PROJECT_ID

    def test_gemini_api_enabled(self):
        serviceusage_client = mock.MagicMock()
        serviceusage_client.active_services = {
            GCP_PROJECT_ID: [
                Service(
                    name="storage.googleapis.com",
                    title="Cloud Storage API",
                    project_id=GCP_PROJECT_ID,
                ),
                Service(
                    name="generativelanguage.googleapis.com",
                    title="Gemini (Generative Language) API",
                    project_id=GCP_PROJECT_ID,
                ),
            ]
        }
        serviceusage_client.project_ids = [GCP_PROJECT_ID]
        serviceusage_client.region = "global"
        serviceusage_client.projects = {
            GCP_PROJECT_ID: GCPProject(
                id=GCP_PROJECT_ID,
                number="123456789012",
                name="test",
                labels={},
                lifecycle_state="ACTIVE",
            )
        }

        with (
            mock.patch(
                "prowler.providers.common.provider.Provider.get_global_provider",
                return_value=set_mocked_gcp_provider(),
            ),
            mock.patch(
                "prowler.providers.gcp.services.gemini.gemini_api_disabled.gemini_api_disabled.serviceusage_client",
                new=serviceusage_client,
            ),
        ):
            from prowler.providers.gcp.services.gemini.gemini_api_disabled.gemini_api_disabled import (
                gemini_api_disabled,
            )

            check = gemini_api_disabled()
            result = check.execute()

            assert len(result) == 1
            assert result[0].status == "FAIL"
            assert (
                result[0].status_extended
                == f"Gemini (Generative Language) API is enabled for project {GCP_PROJECT_ID}"
            )
            assert result[0].resource_id == "generativelanguage.googleapis.com"
            assert result[0].project_id == GCP_PROJECT_ID
