from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_v492_external_sync_service_stages_candidates_not_opportunities():
    service = read("app/external_reconciliation_sync.py")

    assert "sync_jasmin_reconciliation_candidates" in service
    assert "sync_odoo_reconciliation_candidates" in service
    assert "sync_packlink_reconciliation_candidates" in service
    assert "upsert_reconciliation_item" in service
    assert "create_opportunity" not in service
    assert "CONFIRM_PAYMENT" in service
    assert "não" not in service.lower() or "confirm" in service.lower()


def test_v492_jasmin_odoo_packlink_types_are_supported():
    service = read("app/external_reconciliation_sync.py")
    reconciliation = read("app/reconciliation_service.py")
    page = read("app/admin_ui/pages/reconciliation.py")

    assert "jasmin_quotation" in service
    assert "jasmin_invoice" in service
    assert "odoo_sale_order" in service
    assert "packlink_shipment" in service
    assert "packlink_shipment" in reconciliation
    assert "Envio Packlink" in page


def test_v492_reconciliation_page_has_external_sync_actions():
    page = read("app/admin_ui/pages/reconciliation.py")

    assert 'action="/reconciliation/sync-jasmin"' in page
    assert 'action="/reconciliation/sync-odoo"' in page
    assert 'action="/reconciliation/sync-packlink"' in page
    assert 'action="/reconciliation/sync-external"' in page
    assert "Sincronizar APIs externas" in page


def test_v492_sync_script_exists_and_reports_json_summary():
    script = read("scripts/sync_external_reconciliation.py")

    assert "--all" in script
    assert "--jasmin" in script
    assert "--odoo" in script
    assert "--packlink" in script
    assert "created_or_updated" in script
    assert "json.dumps" in script


def test_v492_documentation_describes_operator_confirmation():
    doc = read("docs/CLIENTFLOW_V492_EXTERNAL_API_SYNC.md")

    assert "External API Sync" in doc
    assert "não cria oportunidades automaticamente" in doc.lower()
    assert "não confirma pagamentos" in doc.lower()
    assert "operador decide" in doc.lower()
    assert "systemd" in doc
