"""Oracle Fusion / E-Business Suite — the field table.

Oracle's purchasing tables carry the PO number in `SEGMENT1`, which is a column name that
tells you nothing about what it holds; payables uses `INVOICE_NUM`, receiving uses
`RECEIPT_NUM`. `ORG_ID` (EBS) and `BusinessUnitId` (Fusion) are the operating unit and take
the company-code role.

Both product lines are covered by one table on purpose: the two vocabularies overlap and
neither ever uses the other's name for a different fact, so a merged priority list is
correct and a second adapter would be two tables to keep in step.
"""

from __future__ import annotations

from .base import ErpAdapter, FieldMap

ORACLE_FUSION = FieldMap(
    system_id="ORACLE-FUSION",
    company_code=("ORG_ID", "BusinessUnitId", "BUSINESS_UNIT", "OrganizationCode"),
    document_no=("SEGMENT1", "PO_NUMBER", "PoNumber", "INVOICE_NUM", "InvoiceNumber",
                 "RECEIPT_NUM", "ReceiptNumber", "SHIPMENT_NUM", "CHECK_NUMBER"),
    fiscal_year=("FISCAL_YEAR", "PeriodYear"),
    doc_date=("TRANSACTION_DATE", "INVOICE_DATE", "GL_DATE", "CreationDate", "TransactionDate"),
    amount=("INVOICE_AMOUNT", "PO_AMOUNT", "AMOUNT", "TransactionAmount"),
    currency=("INVOICE_CURRENCY_CODE", "CURRENCY_CODE", "CurrencyCode"),
    doc_type=("DOCUMENT_TYPE_CODE", "TYPE_LOOKUP_CODE", "NEOEDGE_DOCTYPE"),
    lines=("LINES", "Lines", "items"),
    line_sku=("ITEM_NUMBER", "ItemNumber", "sku"),
    line_quantity=("QUANTITY", "QUANTITY_RECEIVED", "Quantity", "quantity"),
    line_unit=("UOM_CODE", "UnitOfMeasure", "unit"),
    line_unit_price=("UNIT_PRICE", "UnitPrice", "unit_price"),
    doc_kinds={
        "STANDARD": "PURCHASE_ORDER", "PO_STANDARD": "PURCHASE_ORDER",
        "ACKNOWLEDGMENT": "ORDER_CONFIRMATION",
        "SHIPMENT": "DELIVERY_NOTE", "ASN": "DELIVERY_NOTE",
        "RECEIVE": "GOODS_RECEIPT", "RECEIPT": "GOODS_RECEIPT",
        "INVOICE": "INVOICE", "STANDARD_INVOICE": "INVOICE",
        "PAYMENT": "PAYMENT",
        "CREDIT": "CREDIT_MEMO", "CREDIT_MEMO": "CREDIT_MEMO",
        "BLANKET": "CONTRACT", "AGREEMENT": "CONTRACT",
    },
)


def adapter() -> ErpAdapter:
    return ErpAdapter(ORACLE_FUSION)
