"""Smoke test for the logframe-wide dashboard (FRMFL032)."""
from __future__ import annotations

from datetime import date
from decimal import Decimal

import pytest
from django.contrib.auth import get_user_model
from django.test import Client

from apps.core.permissions.roles import UserRole
from apps.mel.indicators.models import (
    Indicator,
    IndicatorFrequency,
    IndicatorTarget,
    LogFrame,
    LogFrameLevel,
    LogFrameRow,
)
from apps.mel.indicators.services import record_data_point

pytestmark = pytest.mark.django_db

User = get_user_model()


def test_dashboard_renders_200_with_rollup_for_seeded_logframe():
    officer = User.objects.create_user(email="dashboard-officer@example.com", password="x")
    officer.role = UserRole.MEL_OFFICER.value
    officer.save(update_fields=["role"])

    lf = LogFrame.objects.create(name="Dashboard LF", slug="dashboard-lf")
    impact = LogFrameRow.objects.create(logframe=lf, level=LogFrameLevel.IMPACT, title="I")
    outcome = LogFrameRow.objects.create(logframe=lf, level=LogFrameLevel.OUTCOME, title="O", parent=impact)
    output = LogFrameRow.objects.create(logframe=lf, level=LogFrameLevel.OUTPUT, title="P", parent=outcome)
    ind = Indicator.objects.create(
        logframe_row=output,
        code="dash-ind",
        name="Dashboard indicator",
        data_source="m",
        unit="count",
        calculation_method="count",
        frequency=IndicatorFrequency.MONTHLY,
    )
    IndicatorTarget.objects.create(
        indicator=ind,
        period_label="2026-04",
        period_start=date(2026, 4, 1),
        period_end=date(2026, 4, 30),
        baseline_value=Decimal("0"),
        target_value=Decimal("100"),
    )
    record_data_point(
        indicator=ind, value=90, period_label="2026-04", auto_verify=True,
        source_module="m", source_event="e", source_object_id="1",
    )

    c = Client()
    c.force_login(officer)
    response = c.get(f"/mel/indicators/logframes/{lf.slug}/dashboard/?period=2026-04")

    assert response.status_code == 200
    body = response.content.decode()
    assert "Dashboard LF" in body
    assert "RAG rollup" in body
