# [DEF:backend.src.services.__tests__.test_llm_plugin_persistence:Module]
# @TIER: STANDARD
# @PURPOSE: Regression test for ValidationRecord persistence fields populated from task context.

import types
import pytest

from src.plugins.llm_analysis import plugin as plugin_module


# [DEF:_DummyLogger:Class]
# @PURPOSE: Minimal logger shim for TaskContext-like objects used in tests.
class _DummyLogger:
    def with_source(self, _source: str):
        return self

    def info(self, *_args, **_kwargs):
        return None

    def debug(self, *_args, **_kwargs):
        return None

    def warning(self, *_args, **_kwargs):
        return None

    def error(self, *_args, **_kwargs):
        return None
# [/DEF:_DummyLogger:Class]


# [DEF:_FakeDBSession:Class]
# @PURPOSE: Captures persisted records for assertion and mimics SQLAlchemy session methods used by plugin.
class _FakeDBSession:
    def __init__(self):
        self.added = None
        self.committed = False
        self.closed = False

    def add(self, obj):
        self.added = obj

    def commit(self):
        self.committed = True

    def close(self):
        self.closed = True
# [/DEF:_FakeDBSession:Class]


# [DEF:test_dashboard_validation_plugin_persists_task_and_environment_ids:Function]
# @PURPOSE: Ensure db ValidationRecord includes context.task_id and params.environment_id.
@pytest.mark.asyncio
async def test_dashboard_validation_plugin_persists_task_and_environment_ids(tmp_path, monkeypatch):
    fake_db = _FakeDBSession()

    env = types.SimpleNamespace(id="env-42")
    provider = types.SimpleNamespace(
        id="provider-1",
        name="Main LLM",
        provider_type="openai",
        base_url="https://example.invalid/v1",
        default_model="gpt-4o",
        is_active=True,
    )

    class _FakeProviderService:
        def __init__(self, _db):
            return None

        def get_provider(self, _provider_id):
            return provider

        def get_decrypted_api_key(self, _provider_id):
            return "a" * 32

    class _FakeScreenshotService:
        def __init__(self, _env):
            return None

        async def capture_dashboard(self, _dashboard_id, _screenshot_path):
            return None

    class _FakeLLMClient:
        def __init__(self, **_kwargs):
            return None

        async def analyze_dashboard(self, *_args, **_kwargs):
            return {
                "status": "PASS",
                "summary": "Dashboard healthy",
                "issues": [],
            }

    class _FakeNotificationService:
        def __init__(self, *_args, **_kwargs):
            return None

        async def dispatch_report(self, **_kwargs):
            return None

    class _FakeConfigManager:
        def get_environment(self, _env_id):
            return env

        def get_config(self):
            return types.SimpleNamespace(
                settings=types.SimpleNamespace(
                    storage=types.SimpleNamespace(root_path=str(tmp_path)),
                    llm={},
                )
            )

    class _FakeSupersetClient:
        def __init__(self, _env):
            self.network = types.SimpleNamespace(request=lambda **_kwargs: {"result": []})

    monkeypatch.setattr(plugin_module, "SessionLocal", lambda: fake_db)
    monkeypatch.setattr(plugin_module, "LLMProviderService", _FakeProviderService)
    monkeypatch.setattr(plugin_module, "ScreenshotService", _FakeScreenshotService)
    monkeypatch.setattr(plugin_module, "LLMClient", _FakeLLMClient)
    monkeypatch.setattr(plugin_module, "NotificationService", _FakeNotificationService)
    monkeypatch.setattr(plugin_module, "SupersetClient", _FakeSupersetClient)
    monkeypatch.setattr("src.dependencies.get_config_manager", lambda: _FakeConfigManager())

    context = types.SimpleNamespace(
        task_id="task-999",
        logger=_DummyLogger(),
        background_tasks=None,
    )

    plugin = plugin_module.DashboardValidationPlugin()
    result = await plugin.execute(
        {
            "dashboard_id": "11",
            "environment_id": "env-42",
            "provider_id": "provider-1",
        },
        context=context,
    )

    assert result["environment_id"] == "env-42"
    assert fake_db.committed is True
    assert fake_db.closed is True
    assert fake_db.added is not None
    assert fake_db.added.task_id == "task-999"
    assert fake_db.added.environment_id == "env-42"
# [/DEF:test_dashboard_validation_plugin_persists_task_and_environment_ids:Function]


# [/DEF:backend.src.services.__tests__.test_llm_plugin_persistence:Module]