import pytest
from fastapi import HTTPException

from api.v1.modules.ops.shipments import service
from api.v1.modules.ops.shipments.dto import ShipmentCreate

T, C = 1, 1


def test_seed_import_milestones(db):
    s = service.create_shipment(db, ShipmentCreate(reference="IMP-1", operation_type="importacion"), T, C)
    events = service.seed_default_milestones(db, s.id, T, C)
    titles = [e.event_type for e in events]
    assert "aviso_llegada" in titles
    assert "despacho_importacion" in titles
    assert "entrega" in titles
    # idempotente: no duplica
    again = service.seed_default_milestones(db, s.id, T, C)
    assert len(again) == len(events)


def test_seed_export_milestones_and_complete(db):
    s = service.create_shipment(db, ShipmentCreate(reference="EXP-1", operation_type="exportacion"), T, C)
    events = service.seed_default_milestones(db, s.id, T, C)
    assert any(e.event_type == "embarque" for e in events)
    done = service.complete_shipment_event(db, events[0].id, T, C)
    assert done.status == "completado" and done.actual_date is not None


def test_seed_requires_operation_type(db):
    s = service.create_shipment(db, ShipmentCreate(reference="X-1"), T, C)  # sin operation_type
    with pytest.raises(HTTPException) as exc:
        service.seed_default_milestones(db, s.id, T, C)
    assert exc.value.status_code == 422
