"""The /computers/display-kiosks picker: reporting displays with a derived FQDN.

Feeds the Dashboard Defaults kiosk dropdown so an admin picks a display instead
of typing an IP. The FQDN is F<serial>.<domain> (device naming), derived from the
BIOS serial the collector already reports.
"""
import pytest

from shopdb.core.models import AssetType
from plugins.computers.models import ComputerType

KEY = 'testcollectorkey'


@pytest.fixture
def collector_key(app):
    old = app.config.get('COLLECTOR_API_KEY')
    app.config['COLLECTOR_API_KEY'] = KEY
    yield KEY
    app.config['COLLECTOR_API_KEY'] = old


def test_display_kiosks_derives_fqdn(client, db, collector_key):
    db.session.add(AssetType(assettype='computer', pluginname='computers',
                             tablename='computers'))
    db.session.add(ComputerType(computertype='Kiosk'))
    db.session.commit()

    # a display reports in via the collector (pctype -> Kiosk type, with serial)
    resp = client.post('/api/collector/computers', json={
        'hostname': 'WJDISP01', 'serialnumber': 'ABC1234',
        'pctype': 'gea-shopfloor-display',
    }, headers={'X-API-Key': KEY})
    assert resp.status_code == 200, resp.data

    rows = client.get('/api/computers/display-kiosks').get_json()['data']
    match = [k for k in rows if k['hostname'] == 'WJDISP01']
    assert match, rows
    assert match[0]['fqdn'] == 'fabc1234.device.geaerospace.net'
    assert match[0]['serialnumber'] == 'ABC1234'


def test_display_kiosks_empty_when_no_kiosk_type(client, db):
    # No 'Kiosk' ComputerType -> empty list, not an error.
    assert client.get('/api/computers/display-kiosks').get_json()['data'] == []
