from sqlalchemy.orm import Session
from api.v1.modules.a76.invoices.models import InvoiceFinancials, InvoiceHeader, InvoiceLogistics
from core.exceptions import ErrorCollector

from .. import schemas



def apply_calculations(
    invoice: schemas.InvoiceHeaderUpdate,
):
    if invoice.invoice_type == "CR":
        invoice.compliance_mx.is_regime_change = True
    else:
        invoice.compliance_mx.is_regime_change = False        
        
    increments_me = (invoice.financials.freight or 0) + (invoice.financials.insurance or 0) + (invoice.financials.packaging or 0) + (invoice.financials.other_increments or 0)    
    if invoice.financials.currency == "foreign":
        invoice.financials.total_increments_me = increments_me
        invoice.financials.total_increments_mn = invoice.financials.total_increments_me * invoice.financials.exchange_rate
        invoice.financials.currency_type = "USD"
    elif invoice.financials.currency == "local":
        invoice.financials.total_increments_mn = increments_me
        invoice.financials.total_increments_me = invoice.financials.total_increments_mn / invoice.financials.exchange_rate
        invoice.financials.currency_type = "MXN"
    elif invoice.financials.currency == "manual":
        invoice.financials.total_increments_me = (increments_me)/invoice.financials.exchange_rate
        invoice.financials.total_increments_mn = invoice.financials.total_increments_me * invoice.financials.exchange_rate
        
            
    invoice.compliance_mx.is_pedimento_pending = False
    if not invoice.compliance_mx.pedimento_id:
        invoice.compliance_mx.is_pedimento_pending = True
        
    
        
   