"""
Workflow Entity Types - Standard entity type codes for workflow management

This module provides standardized entity type codes used in workflow instances
to identify the type of transaction/document being processed through approval workflows.

IFRS Compliant - These entity types map to transaction types requiring proper
authorization and segregation of duties as per IFRS standards.
"""

# Procurement Module Entity Types
PURCHASE_REQUISITION = "purchase_requisition"
PURCHASE_ORDER = "purchase_order"
GOODS_RECEIPT = "goods_receipt"
VENDOR_PAYMENT = "vendor_payment"

# Sales Module Entity Types
SALES_QUOTATION = "sales_quotation"
SALES_ORDER = "sales_order"
SALES_INVOICE = "sales_invoice"
SALES_CREDIT_NOTE = "sales_credit_note"
CUSTOMER_CREDIT_LIMIT = "customer_credit_limit"

# Inventory Module Entity Types
INVENTORY_ADJUSTMENT = "inventory_adjustment"
STOCK_WRITEOFF = "stock_writeoff"
STOCK_TRANSFER = "stock_transfer"
INVENTORY_REVALUATION = "inventory_revaluation"

# Manufacturing Module Entity Types
BOM_CHANGE = "bom_change"
PRODUCTION_ORDER = "production_order"
PRODUCTION_VARIANCE = "production_variance"
SCRAP_DISPOSAL = "scrap_disposal"

# Accounting Module Entity Types
JOURNAL_ENTRY = "journal_entry"
ACCRUAL = "accrual"
BAD_DEBT_WRITEOFF = "bad_debt_writeoff"
PERIOD_CLOSE = "period_close"
COA_CHANGE = "coa_change"
INTERCOMPANY = "intercompany"

# Fixed Assets Module Entity Types
ASSET_ACQUISITION = "asset_acquisition"
ASSET_DISPOSAL = "asset_disposal"
ASSET_IMPAIRMENT = "asset_impairment"
DEPRECIATION_POLICY = "depreciation_policy"

# Banking Module Entity Types
BANK_RECON_ADJ = "bank_recon_adj"
CASH_TRANSFER = "cash_transfer"
PETTY_CASH = "petty_cash"

# Tax Module Entity Types
VAT_RETURN = "vat_return"
TAX_PAYMENT = "tax_payment"

# HR/Payroll Module Entity Types
PAYROLL_RUN = "payroll_run"
EXPENSE_CLAIM = "expense_claim"

# Budgeting Module Entity Types
BUDGET = "budget"
BUDGET_TRANSFER = "budget_transfer"


# Human-readable labels for each entity type
ENTITY_TYPE_LABELS = {
    # Procurement
    PURCHASE_REQUISITION: "Purchase Requisition",
    PURCHASE_ORDER: "Purchase Order",
    GOODS_RECEIPT: "Goods Receipt",
    VENDOR_PAYMENT: "Vendor Payment",

    # Sales
    SALES_QUOTATION: "Sales Quotation",
    SALES_ORDER: "Sales Order",
    SALES_INVOICE: "Sales Invoice",
    SALES_CREDIT_NOTE: "Sales Credit Note",
    CUSTOMER_CREDIT_LIMIT: "Customer Credit Limit",

    # Inventory
    INVENTORY_ADJUSTMENT: "Inventory Adjustment",
    STOCK_WRITEOFF: "Stock Write-off",
    STOCK_TRANSFER: "Stock Transfer",
    INVENTORY_REVALUATION: "Inventory Revaluation",

    # Manufacturing
    BOM_CHANGE: "BOM Change",
    PRODUCTION_ORDER: "Production Order",
    PRODUCTION_VARIANCE: "Production Variance",
    SCRAP_DISPOSAL: "Scrap Disposal",

    # Accounting
    JOURNAL_ENTRY: "Journal Entry",
    ACCRUAL: "Accrual Entry",
    BAD_DEBT_WRITEOFF: "Bad Debt Write-off",
    PERIOD_CLOSE: "Period Close",
    COA_CHANGE: "Chart of Accounts Change",
    INTERCOMPANY: "Inter-Company Transaction",

    # Fixed Assets
    ASSET_ACQUISITION: "Asset Acquisition",
    ASSET_DISPOSAL: "Asset Disposal",
    ASSET_IMPAIRMENT: "Asset Impairment",
    DEPRECIATION_POLICY: "Depreciation Policy Change",

    # Banking
    BANK_RECON_ADJ: "Bank Reconciliation Adjustment",
    CASH_TRANSFER: "Cash Transfer",
    PETTY_CASH: "Petty Cash Replenishment",

    # Tax
    VAT_RETURN: "VAT Return",
    TAX_PAYMENT: "Tax Payment",

    # HR/Payroll
    PAYROLL_RUN: "Payroll Run",
    EXPENSE_CLAIM: "Expense Claim",

    # Budgeting
    BUDGET: "Budget",
    BUDGET_TRANSFER: "Budget Transfer",
}


# Module mapping - which module each entity type belongs to
ENTITY_TYPE_MODULES = {
    # Procurement
    PURCHASE_REQUISITION: "procurement",
    PURCHASE_ORDER: "procurement",
    GOODS_RECEIPT: "procurement",
    VENDOR_PAYMENT: "procurement",

    # Sales
    SALES_QUOTATION: "sales",
    SALES_ORDER: "sales",
    SALES_INVOICE: "sales",
    SALES_CREDIT_NOTE: "sales",
    CUSTOMER_CREDIT_LIMIT: "sales",

    # Inventory
    INVENTORY_ADJUSTMENT: "inventory",
    STOCK_WRITEOFF: "inventory",
    STOCK_TRANSFER: "inventory",
    INVENTORY_REVALUATION: "inventory",

    # Manufacturing
    BOM_CHANGE: "manufacturing",
    PRODUCTION_ORDER: "manufacturing",
    PRODUCTION_VARIANCE: "manufacturing",
    SCRAP_DISPOSAL: "manufacturing",

    # Accounting
    JOURNAL_ENTRY: "accounting",
    ACCRUAL: "accounting",
    BAD_DEBT_WRITEOFF: "accounting",
    PERIOD_CLOSE: "accounting",
    COA_CHANGE: "accounting",
    INTERCOMPANY: "accounting",

    # Fixed Assets
    ASSET_ACQUISITION: "assets",
    ASSET_DISPOSAL: "assets",
    ASSET_IMPAIRMENT: "assets",
    DEPRECIATION_POLICY: "assets",

    # Banking
    BANK_RECON_ADJ: "banking",
    CASH_TRANSFER: "banking",
    PETTY_CASH: "banking",

    # Tax
    VAT_RETURN: "tax",
    TAX_PAYMENT: "tax",

    # HR/Payroll
    PAYROLL_RUN: "payroll",
    EXPENSE_CLAIM: "payroll",

    # Budgeting
    BUDGET: "budgeting",
    BUDGET_TRANSFER: "budgeting",
}


# IFRS Standard mappings for each entity type
ENTITY_TYPE_IFRS = {
    # Procurement
    PURCHASE_REQUISITION: ["IAS 2"],
    PURCHASE_ORDER: ["IAS 2", "IAS 16", "IAS 37"],
    GOODS_RECEIPT: ["IAS 2"],
    VENDOR_PAYMENT: ["IAS 7", "IFRS 9"],

    # Sales
    SALES_QUOTATION: ["IFRS 15"],
    SALES_ORDER: ["IFRS 15"],
    SALES_INVOICE: ["IFRS 15"],
    SALES_CREDIT_NOTE: ["IFRS 15"],
    CUSTOMER_CREDIT_LIMIT: ["IFRS 9"],

    # Inventory
    INVENTORY_ADJUSTMENT: ["IAS 2"],
    STOCK_WRITEOFF: ["IAS 2"],
    STOCK_TRANSFER: ["IAS 2"],
    INVENTORY_REVALUATION: ["IAS 2"],

    # Manufacturing
    BOM_CHANGE: ["IAS 2"],
    PRODUCTION_ORDER: ["IAS 2"],
    PRODUCTION_VARIANCE: ["IAS 2"],
    SCRAP_DISPOSAL: ["IAS 2"],

    # Accounting
    JOURNAL_ENTRY: ["IAS 8"],
    ACCRUAL: ["IAS 37"],
    BAD_DEBT_WRITEOFF: ["IFRS 9"],
    PERIOD_CLOSE: ["IAS 1"],
    COA_CHANGE: ["IAS 1"],
    INTERCOMPANY: ["IFRS 10", "IAS 24"],

    # Fixed Assets
    ASSET_ACQUISITION: ["IAS 16"],
    ASSET_DISPOSAL: ["IAS 16"],
    ASSET_IMPAIRMENT: ["IAS 36"],
    DEPRECIATION_POLICY: ["IAS 8"],

    # Banking
    BANK_RECON_ADJ: ["IAS 7"],
    CASH_TRANSFER: ["IAS 7"],
    PETTY_CASH: ["IAS 7"],

    # Tax
    VAT_RETURN: ["IAS 12"],
    TAX_PAYMENT: ["IAS 12"],

    # HR/Payroll
    PAYROLL_RUN: ["IAS 19"],
    EXPENSE_CLAIM: ["IAS 19"],

    # Budgeting
    BUDGET: ["IAS 1"],
    BUDGET_TRANSFER: ["IAS 1"],
}


# Phase 1 Critical Workflows - Immediate implementation required
PHASE1_ENTITY_TYPES = [
    JOURNAL_ENTRY,
    VENDOR_PAYMENT,
    SALES_CREDIT_NOTE,
    BAD_DEBT_WRITEOFF,
    PERIOD_CLOSE,
]


# Get entity type label helper function
def get_entity_label(entity_type: str) -> str:
    """Get human-readable label for an entity type"""
    return ENTITY_TYPE_LABELS.get(entity_type, entity_type.replace("_", " ").title())


# Get entity module helper function
def get_entity_module(entity_type: str) -> str:
    """Get module for an entity type"""
    return ENTITY_TYPE_MODULES.get(entity_type, "general")


# Get IFRS standards helper function
def get_entity_ifrs(entity_type: str) -> list:
    """Get IFRS standards related to an entity type"""
    return ENTITY_TYPE_IFRS.get(entity_type, [])
