"""Tests for the ADR-010 frontend-contribution consumers (/api/pluginui/*).

Pins the wiring added for the four data-only presentation hooks
(get_settings_cards, get_asset_panels, get_map_overlays,
get_asset_presentation): each endpoint aggregates enabled plugins'
contributions, tags them with the plugin name, and drops disabled plugins.
Also asserts the pilot plugins (warranty, measuringtools) contribute.

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


def _data(client, path, headers):
    response = client.get(path, headers=headers)
    assert response.status_code == 200, response.get_json()
    payload = response.get_json()['data']
    assert isinstance(payload, list)
    return payload


# =============================================================================
# settings-cards
# =============================================================================

def test_settings_cards_aggregate_enabled_plugins(app, client, auth_headers, monkeypatch):
    """measuringtools contributes its settings card, tagged + position-sorted."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: True)

    cards = _data(client, '/api/pluginui/settings-cards', auth_headers)
    plugins = {c.get('plugin') for c in cards}
    assert 'measuringtools' in plugins
    card = next(c for c in cards if c['plugin'] == 'measuringtools')
    assert card['group'] == 'Measuring Tools'
    assert card['to'] == '/settings/measuringtooltypes'
    positions = [c.get('position', 99) for c in cards]
    assert positions == sorted(positions)


def test_settings_cards_skip_disabled_plugin(app, client, auth_headers, monkeypatch):
    """A disabled measuringtools drops its settings card."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: name != 'measuringtools')

    cards = _data(client, '/api/pluginui/settings-cards', auth_headers)
    assert 'measuringtools' not in {c.get('plugin') for c in cards}


# =============================================================================
# asset-panels
# =============================================================================

def _make_asset(db):
    from shopdb.core.models import Asset, AssetType
    atype = AssetType.query.filter_by(assettype='computer').first()
    if not atype:
        atype = AssetType(assettype='computer', pluginname='computer',
                          tablename='computer', description='c')
        db.session.add(atype)
        db.session.commit()
    asset = Asset(assetnumber='AST-PANEL01', assettypeid=atype.assettypeid,
                  isactive=True)
    db.session.add(asset)
    db.session.commit()
    return asset.assetid


def test_asset_panels_require_assetid(client, auth_headers):
    """asset-panels without assetid is a 400."""
    response = client.get('/api/pluginui/asset-panels', headers=auth_headers)
    assert response.status_code == 400


def test_asset_panels_match_asset_type(app, client, db, auth_headers, monkeypatch):
    """Warranty's ['*'] panel matches any asset; tagged with its plugin."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: True)
    assetid = _make_asset(db)

    panels = _data(client, f'/api/pluginui/asset-panels?assetid={assetid}', auth_headers)
    warranty = next((p for p in panels if p.get('plugin') == 'warranty'), None)
    assert warranty is not None, 'warranty asset panel missing'
    assert warranty['id'] == 'warranty'
    assert warranty['endpoint'] == '/api/warranty/asset/{assetid}'
    assert warranty['render'] in ('keyvalue', 'table', 'badge', 'list')


def test_asset_panels_skip_disabled_plugin(app, client, db, auth_headers, monkeypatch):
    """A disabled warranty drops its asset panel."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: name != 'warranty')
    assetid = _make_asset(db)

    panels = _data(client, f'/api/pluginui/asset-panels?assetid={assetid}', auth_headers)
    assert 'warranty' not in {p.get('plugin') for p in panels}


# =============================================================================
# map-overlays
# =============================================================================

def test_map_overlays_aggregate_enabled_plugins(app, client, auth_headers, monkeypatch):
    """measuringtools contributes its calibration-due overlay."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: True)

    overlays = _data(client, '/api/pluginui/map-overlays', auth_headers)
    overlay = next((o for o in overlays if o.get('plugin') == 'measuringtools'), None)
    assert overlay is not None
    assert overlay['id'] == 'calibration-due'
    assert overlay['legend'] is True


def test_map_overlays_skip_disabled_plugin(app, client, auth_headers, monkeypatch):
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: name != 'measuringtools')

    overlays = _data(client, '/api/pluginui/map-overlays', auth_headers)
    assert 'measuringtools' not in {o.get('plugin') for o in overlays}


# =============================================================================
# asset-presentation
# =============================================================================

def test_asset_presentation_aggregate_enabled_plugins(app, client, auth_headers, monkeypatch):
    """measuringtools declares presentation for its measuring_tool type."""
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: True)

    entries = _data(client, '/api/pluginui/asset-presentation', auth_headers)
    entry = next((e for e in entries if e.get('assettype') == 'measuring_tool'), None)
    assert entry is not None
    assert entry['plugin'] == 'measuringtools'
    assert entry['route'] == '/measuringtools/by-asset/{assetid}'


def test_asset_presentation_skip_disabled_plugin(app, client, auth_headers, monkeypatch):
    pm = app.extensions['plugin_manager']
    monkeypatch.setattr(pm.registry, 'is_enabled', lambda name: name != 'measuringtools')

    entries = _data(client, '/api/pluginui/asset-presentation', auth_headers)
    assert 'measuring_tool' not in {e.get('assettype') for e in entries}
