"""Tests for the printer installer endpoint (/api/printers/install-list).

The signed printer installer EXE pulls network-printer data + floor-map
positions from here (shopfloor 2.0 cannot run unsigned .bat maps).
"""

import pytest


@pytest.fixture
def printer_assettype(db):
    from shopdb.core.models import AssetType
    at = AssetType(assettype='printer', pluginname='printers',
                   tablename='printers', description='Printers')
    db.session.add(at)
    db.session.commit()
    return at


def test_install_list_returns_network_printer_with_map(client, db, auth_headers,
                                                       printer_assettype):
    """A network printer appears with flat installer fields incl map position."""
    created = client.post('/api/printers', json={
        'assetnumber': 'CSF01-Materials-HP',
        'hostname': 'wjprn01',
        'installpath': r'\\srv\drivers\hp',
        'mapx': 120, 'mapy': 240,
    }, headers=auth_headers)
    assert created.status_code == 201, created.get_json()

    resp = client.get('/api/printers/install-list', headers=auth_headers)
    assert resp.status_code == 200
    rows = resp.get_json()['data']
    row = next((r for r in rows if r['machinenumber'] == 'CSF01-Materials-HP'), None)
    assert row is not None
    assert row['hostname'] == 'wjprn01'
    assert row['installpath'] == r'\\srv\drivers\hp'
    assert row['mapx'] == 120 and row['mapy'] == 240


def test_install_list_excludes_usb_only_printer(client, db, auth_headers,
                                                printer_assettype):
    """A printer with no hostname and no network IP is excluded."""
    client.post('/api/printers', json={'assetnumber': 'USB-LABELER'},
                headers=auth_headers)
    resp = client.get('/api/printers/install-list', headers=auth_headers)
    machinenumbers = [r['machinenumber'] for r in resp.get_json()['data']]
    assert 'USB-LABELER' not in machinenumbers
