from decimal import Decimal

from api.v1.modules.crm.accounts import service as accounts_service
from api.v1.modules.crm.accounts.dto import AccountCreate
from api.v1.modules.crm.quotes import service as quotes_service
from api.v1.modules.crm.quotes.dto import QuoteCreate, QuoteItemCreate
from api.v1.modules.fin.invoices import service
from api.v1.modules.fin.invoices.dto import InvoiceCreate, InvoiceItemCreate, PaymentCreate
from api.v1.modules.ops.shipments import service as shipments_service
from api.v1.modules.ops.shipments.dto import ShipmentCloseInput, ShipmentCreate

T, C = 1, 1


def test_invoice_totals_with_tax(db):
    inv = service.create_invoice(db, InvoiceCreate(reference="F-001", currency="MXN", tax_rate=Decimal("16")), T, C)
    service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="flete_internacional", quantity=1, unit_amount=1000), T, C)
    service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="despacho_aduanal", quantity=1, unit_amount=500), T, C)
    inv = service.get_invoice(db, inv.id, T, C)
    assert float(inv.subtotal) == 1500.0
    assert float(inv.tax_amount) == 240.0  # 16% de 1500
    assert float(inv.total) == 1740.0
    assert float(inv.balance) == 1740.0


def test_payment_marks_paid(db):
    inv = service.create_invoice(db, InvoiceCreate(reference="F-002", tax_rate=Decimal("0")), T, C)
    service.create_item(db, InvoiceItemCreate(invoice_id=inv.id, concept="otros", quantity=1, unit_amount=1000), T, C)
    service.emit_invoice(db, inv.id, T, C)
    service.create_payment(db, PaymentCreate(invoice_id=inv.id, amount=Decimal("400"), method="transferencia"), T, C)
    inv = service.get_invoice(db, inv.id, T, C)
    assert float(inv.paid_amount) == 400.0 and float(inv.balance) == 600.0
    assert inv.status == "emitida"
    service.create_payment(db, PaymentCreate(invoice_id=inv.id, amount=Decimal("600")), T, C)
    inv = service.get_invoice(db, inv.id, T, C)
    assert float(inv.balance) == 0.0 and inv.status == "pagada" and inv.paid_at is not None


def test_generate_from_shipment_copies_quote_items(db):
    acc = accounts_service.create_account(db, AccountCreate(name="Cliente"), T, C)
    quote = quotes_service.create_quote(db, QuoteCreate(reference="COT-9", account_id=acc.id, currency="USD"), T, C)
    quotes_service.create_quote_item(db, QuoteItemCreate(quote_id=quote.id, concept="flete_internacional", quantity=1, unit_cost=1000, unit_sale=1500), T, C)
    quotes_service.accept_quote(db, quote.id, T, C)
    shipment = shipments_service.create_shipment_from_quote(db, quote.id, T, C)
    # El embarque debe cerrarse operativamente antes de facturar (R-F-01)
    shipments_service.close_shipment(
        db, shipment.id, ShipmentCloseInput(actual_cost_total=Decimal("1000"), cost_currency="USD"), T, C
    )

    inv = service.generate_from_shipment(db, shipment.id, T, C)
    assert inv.shipment_id == shipment.id
    assert inv.account_id == acc.id
    assert inv.currency == "USD"
    assert float(inv.ops_cost_total) == 1000.0  # costos de operación arrastrados (R-F-02)
    items = service.get_items(db, inv.id, T, C)
    assert len(items) == 1 and float(items[0].unit_amount) == 1500.0
    assert float(inv.subtotal) == 1500.0
