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

from ....models import LineItem
from ....line_financials.models import LineFinancial
from ....line_financials.schemas import LineFinancialCreate
from ....line_quantities.models import LineQuantity
from ....line_quantities.schemas import LineQuantityCreate
from ....line_customs.models import LineCustom
from ....line_customs.schemas import LineCustomCreate
from ....line_descriptions.models import LineDescription
from ....line_descriptions.schemas import LineDescriptionCreate
from ....line_references.models import LineReference
from api.v1.modules.a24.fa.fa_item_lines.models import FaLineItem
from ....models import LineItem
from api.v1.modules.a76.classes.models import Class


def apply_calculations(
    db: Session, line: LineItem, tenant_id: int, company_id: int, line_number: int
):
    #TODO: SSisGen Logic 
    # if ssisgen.calcularcostounitarioenbaseavalortotalscaf = 1:
    #   unit_cost_capture = line.financial.total_value / line.financial.total_value <-- habria que revisar por que esta asi, por que para mi no tiene sentido, pero es lo que esta en clarion
    caluclate_values(db, line, tenant_id, company_id)
    
    if not line.fa_data.is_subitem:
        line.fa_data.subitem_number = None
        
    invoice_date = db.query(InvoiceHeader.invoice_date).filter(InvoiceHeader.id == line.invoice_id, InvoiceHeader.tenant_id == tenant_id, InvoiceHeader.company_id == company_id).scalar()
        
    line.depreciation_date = invoice_date
    
    if (not line.description.description_spanish and not line.description.description_english) and (line.part_info.description_spanish and line.part_info.description_english):
        line.description.description_spanish = line.part_info.description_spanish
        line.description.description_english = line.part_info.description_english
    else:
        if not line.description.description_spanish:
            class_desc = (
                db.query(Class.description_es, Class.description_en)
                .filter(Class.id == line.class_id, Class.tenant_id == tenant_id, Class.company_id == company_id)
                .first()
            )
            if class_desc:
                line.description.description_spanish, line.description.description_english = class_desc
            

def caluclate_values(
    db: Session, line: LineItem, tenant_id: int, company_id: int
):
    result = (
        db.query(InvoiceFinancials.currency, InvoiceFinancials.exchange_rate)
        .filter(InvoiceFinancials.invoice_id == line.invoice_id, InvoiceFinancials.tenant_id == tenant_id, InvoiceFinancials.company_id == company_id)
        .first()
    )
    if not result:
        return

    currency, exchange_rate = result

    if currency == "foreign":
        line.financial.unit_cost_usd = line.financial.unit_cost_capture
        line.financial.value_usd = line.financial.unit_cost_usd * line.quantity.quantity
        line.financial.unit_cost_mxn = line.financial.unit_cost_capture * exchange_rate
        line.financial.value_mxn = line.financial.unit_cost_mxn * line.quantity.quantity
        line.financial.value_mc = line.financial.unit_cost_usd * line.quantity.quantity
    elif currency == "local":
        line.financial.unit_cost_mxn = line.financial.unit_cost_capture
        line.financial.value_mxn = line.financial.unit_cost_mxn * line.quantity.quantity
        line.financial.unit_cost_usd = line.financial.unit_cost_capture / exchange_rate
        line.financial.value_usd = line.financial.unit_cost_usd * line.quantity.quantity
        line.financial.value_mc = line.financial.unit_cost_usd * line.quantity.quantity
    elif currency == "manual":
        line.financial.unit_cost_usd = line.financial.unit_cost_capture/exchange_rate
        line.financial.value_usd = line.financial.unit_cost_usd * line.quantity.quantity
        line.financial.unit_cost_mxn = line.financial.unit_cost_usd * exchange_rate
        line.financial.value_mxn = line.financial.unit_cost_mxn * line.quantity.quantity
        line.financial.value_mc = line.financial.unit_cost_capture * line.quantity.quantity