"""Vendor-model photo surfaces in asset-extension detail payloads.

Machines, computers, and printers link a vendor Model via modelnumberid; when
that Model carries an imageurl, the extension dict in the GET detail payload
must expose it as 'imageurl' (the field name machines established, consumed by
the detail-page heroes and the machine badge). When no model is linked, the
field must be absent so the frontend v-if renders nothing.

Network devices and measuring tools have no model link, so they are out of
scope here by design.
"""

from shopdb.extensions import db as _db
from shopdb.core.models import Asset, AssetType, Model

from plugins.machines.models import Machine
from plugins.computers.models import Computer
from plugins.printers.models import Printer

IMAGE_URL = '/api/models/images/model-1.png'


def _seed_asset(assetnumber, assettype):
    atype = AssetType.query.filter_by(assettype=assettype).first()
    if not atype:
        atype = AssetType(assettype=assettype)
        _db.session.add(atype)
        _db.session.flush()
    asset = Asset(assetnumber=assetnumber, assettypeid=atype.assettypeid)
    _db.session.add(asset)
    _db.session.flush()
    return asset


def _seed_model(imageurl=IMAGE_URL):
    model = Model(modelnumber='IMG-MODEL-1', imageurl=imageurl)
    _db.session.add(model)
    _db.session.flush()
    return model


def test_machine_detail_carries_model_imageurl(client, db):
    asset = _seed_asset('2001', 'machine')
    model = _seed_model()
    machine = Machine(assetid=asset.assetid, modelnumberid=model.modelnumberid)
    _db.session.add(machine)
    _db.session.commit()

    resp = client.get(f'/api/machines/{machine.machineid}')
    assert resp.status_code == 200, resp.get_json()
    assert resp.get_json()['data']['machine']['imageurl'] == IMAGE_URL


def test_computer_detail_carries_model_imageurl(client, db):
    asset = _seed_asset('PC-2001', 'computer')
    model = _seed_model()
    computer = Computer(assetid=asset.assetid, modelnumberid=model.modelnumberid)
    _db.session.add(computer)
    _db.session.commit()

    resp = client.get(f'/api/computers/{computer.computerid}')
    assert resp.status_code == 200, resp.get_json()
    assert resp.get_json()['data']['computer']['imageurl'] == IMAGE_URL


def test_printer_detail_carries_model_imageurl(client, db):
    asset = _seed_asset('PR-2001', 'printer')
    model = _seed_model()
    printer = Printer(assetid=asset.assetid, modelnumberid=model.modelnumberid)
    _db.session.add(printer)
    _db.session.commit()

    resp = client.get(f'/api/printers/{printer.printerid}')
    assert resp.status_code == 200, resp.get_json()
    assert resp.get_json()['data']['printer']['imageurl'] == IMAGE_URL


def test_detail_omits_imageurl_without_model(client, db):
    """No model linked -> no imageurl key (frontend v-if shows nothing)."""
    asset = _seed_asset('PR-2002', 'printer')
    printer = Printer(assetid=asset.assetid)
    _db.session.add(printer)
    _db.session.commit()

    resp = client.get(f'/api/printers/{printer.printerid}')
    assert resp.status_code == 200, resp.get_json()
    assert 'imageurl' not in resp.get_json()['data']['printer']


def test_detail_omits_imageurl_when_model_has_no_image(client, db):
    """Model linked but no photo -> no imageurl key."""
    asset = _seed_asset('PC-2002', 'computer')
    model = _seed_model(imageurl=None)
    computer = Computer(assetid=asset.assetid, modelnumberid=model.modelnumberid)
    _db.session.add(computer)
    _db.session.commit()

    resp = client.get(f'/api/computers/{computer.computerid}')
    assert resp.status_code == 200, resp.get_json()
    assert 'imageurl' not in resp.get_json()['data']['computer']
