"""Tests for the get_reports hook consumer (GET /api/reports).

Pins the wiring added for BasePlugin.get_reports: the reports list merges
enabled plugins' report cards after the static core reports and skips disabled
ones. Also guards the removed legacy warranty-status core report.

Plugin enabled-state is monkeypatched (not persisted) so these tests do not
mutate the shared instance/plugins.json registry file.
"""


def _report_ids(client, headers):
    response = client.get('/api/reports', headers=headers)
    assert response.status_code == 200, response.get_json()
    reports = response.get_json()['data']['reports']
    assert isinstance(reports, list)
    return reports, {r['id'] for r in reports}


def test_reports_include_core_reports(client, auth_headers):
    """The static core reports are always listed."""
    _, ids = _report_ids(client, auth_headers)
    assert 'machines-by-type' in ids
    assert 'pc-relationships' in ids


def test_legacy_warranty_status_report_absent(client, auth_headers):
    """The dead warranty-status core report was removed."""
    _, ids = _report_ids(client, auth_headers)
    assert 'warranty-status' not in ids


def test_enabled_plugin_reports_are_merged(app, client, auth_headers, monkeypatch):
    """Enabled plugins implementing get_reports contribute their cards."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: True)

    reports, ids = _report_ids(client, auth_headers)
    # warranty + printers implement get_reports
    assert 'warranty' in ids
    assert 'toner' in ids
    # merged cards carry their originating plugin name
    warranty_card = next(r for r in reports if r['id'] == 'warranty')
    assert warranty_card['plugin'] == 'warranty'
    assert warranty_card['route'] == '/reports/warranty'


def test_disabled_plugin_reports_drop_out(app, client, auth_headers, monkeypatch):
    """A disabled plugin's report cards disappear from the list."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: name != 'warranty')

    _, ids = _report_ids(client, auth_headers)
    assert 'warranty' not in ids
    assert 'toner' in ids  # printers still enabled
