from src.services.adr100_slo_metrics_service import (
    Adr100SloMetricsSnapshot,
    AutomationOperationSample,
    QualitySummaryObservation,
    VerificationSample,
    render_adr100_slo_metrics,
)


def test_render_adr100_slo_metrics_outputs_required_series() -> None:
    snapshot = Adr100SloMetricsSnapshot(
        automation_operations=[
            AutomationOperationSample(
                outcome="auto_executed",
                operation_type="playbook_executed",
                count=8,
            ),
            AutomationOperationSample(
                outcome="human_required",
                operation_type="playbook_executed",
                count=2,
            ),
        ],
        automation_operations_24h=[
            AutomationOperationSample(
                outcome="auto_executed",
                operation_type="auto_repair_executed",
                count=3,
            ),
        ],
        post_execution_verifications=[
            VerificationSample(outcome="success", count=7),
            VerificationSample(outcome="failed", count=1),
        ],
        post_execution_verifications_24h=[
            VerificationSample(outcome="success", count=5),
        ],
        knowledge_entries_total=2161,
        knowledge_entries_created_24h=25,
        high_confidence_total=9,
        high_confidence_success_total=7,
        quality_summary_observations=[
            QualitySummaryObservation(
                project_id="awoooi",
                hours=24,
                limit=8,
                cache_status="miss",
                success=True,
                duration_seconds=1.234567,
                observed_at=1_778_756_100,
            ),
        ],
        emitted_at=1_778_756_000,
    )

    rendered = render_adr100_slo_metrics(snapshot)

    assert (
        'automation_operation_log_total{outcome="auto_executed",'
        'operation_type="playbook_executed"} 8'
    ) in rendered
    assert (
        'automation_operation_created_24h{outcome="auto_executed",'
        'operation_type="auto_repair_executed"} 3'
    ) in rendered
    assert 'post_execution_verification_total{outcome="success"} 7' in rendered
    assert 'post_execution_verification_created_24h{outcome="success"} 5' in rendered
    assert "knowledge_entries_total 2161" in rendered
    assert "knowledge_entries_created_24h 25" in rendered
    assert "approval_records_high_confidence_total 9" in rendered
    assert "approval_records_high_confidence_success_total 7" in rendered
    assert "adr100_slo_emitter_last_success_timestamp 1778756000" in rendered
    assert (
        'awooop_truth_chain_quality_summary_last_duration_seconds{project_id="awoooi",'
        'hours="24",limit="8",cache_status="miss",success="true"} 1.234567'
    ) in rendered
    assert (
        'awooop_truth_chain_quality_summary_last_success{project_id="awoooi",'
        'hours="24",limit="8",cache_status="miss",success="true"} 1'
    ) in rendered
    assert (
        'awooop_truth_chain_quality_summary_observed_timestamp{project_id="awoooi",'
        'hours="24",limit="8",cache_status="miss",success="true"} 1778756100'
    ) in rendered


def test_render_adr100_slo_metrics_emits_zero_series_when_empty() -> None:
    rendered = render_adr100_slo_metrics(
        Adr100SloMetricsSnapshot(emitted_at=1_778_756_000),
    )

    assert 'automation_operation_log_total{outcome="none",operation_type="none"} 0' in rendered
    assert 'automation_operation_created_24h{outcome="none",operation_type="none"} 0' in rendered
    assert 'post_execution_verification_total{outcome="none"} 0' in rendered
    assert 'post_execution_verification_created_24h{outcome="none"} 0' in rendered
    assert "knowledge_entries_total 0" in rendered
    assert "knowledge_entries_created_24h 0" in rendered
    assert (
        'awooop_truth_chain_quality_summary_last_duration_seconds{project_id="none",'
        'hours="0",limit="0",cache_status="none",success="false"} 0'
    ) in rendered


def test_render_adr100_slo_metrics_escapes_labels() -> None:
    rendered = render_adr100_slo_metrics(
        Adr100SloMetricsSnapshot(
            automation_operations=[
                AutomationOperationSample(
                    outcome='auto"executed',
                    operation_type="line\nbreak",
                    count=1,
                ),
            ],
            emitted_at=1_778_756_000,
        ),
    )

    assert 'outcome="auto\\"executed"' in rendered
    assert 'operation_type="line\\nbreak"' in rendered
