"""
Industry-specific Chart of Accounts packs.

Each company gets a CORE set + their industry pack during initial setup.
Accountants can add more accounts at any time from the full IFRS catalogue.

Architecture:
  - Global accounts (branch_id=NULL): full 182-code IFRS catalogue — used by system services
  - Company accounts (branch_id=HQ):  industry-filtered subset — shown in the UI
"""

from __future__ import annotations

# ─────────────────────────────────────────────────────────────────────────────
# CORE: accounts every business needs regardless of industry (~78 codes)
# ─────────────────────────────────────────────────────────────────────────────
CORE_ACCOUNTS: frozenset[str] = frozenset({
    # ── ASSETS ───────────────────────────────────────────────────────────────
    "1000",                                         # Assets (root)
    "1100",                                         # Current Assets

    # Cash & Cash Equivalents
    "1110", "1111", "1112", "1113", "1114",

    # Bank Accounts
    "1120", "1121", "1122", "1123", "1124",

    # Trade Receivables
    "1130", "1131", "1132",

    # Prepayments
    "1150", "1151", "1152", "1153", "1154",

    # VAT & Tax Receivable
    "1160", "1161", "1162", "1163", "1164", "1165",

    # Non-Current Assets + PPE
    "1200",
    "1210", "1211", "1212", "1213", "1214", "1215", "1216", "1217",
    "1220", "1221", "1222", "1223", "1224", "1225",

    # ── LIABILITIES ──────────────────────────────────────────────────────────
    "2000",

    # Current Liabilities
    "2100",
    "2110", "2111", "2112", "2113", "2114",         # Trade payables
    "2120", "2121", "2122", "2123",                 # Short-term borrowings
    "2130", "2131", "2132", "2133", "2134", "2135", "2136",  # Tax
    "2140", "2141", "2142", "2143", "2144",         # Other CL

    # Non-Current Liabilities
    "2200",
    "2210", "2211", "2212", "2213",                 # LT borrowings
    "2240",                                          # Deferred tax

    # ── EQUITY ───────────────────────────────────────────────────────────────
    "3000",
    "3100", "3110", "3120",                         # Share capital
    "3200", "3210", "3220", "3230",                 # Retained earnings
    "3300", "3310", "3320", "3330",                 # OCI
    "3400",                                          # Drawings

    # ── REVENUE ──────────────────────────────────────────────────────────────
    "4000",
    "4200", "4210", "4220", "4230", "4240", "4250", "4260", "4270",  # Other income

    # ── EXPENSES ─────────────────────────────────────────────────────────────
    "5000",

    # Administrative Expenses (all businesses need these)
    "5300",
    "5310", "5320",                                 # Salaries & payroll
    "5330",                                         # Rent
    "5340",                                         # Utilities
    "5350",                                         # Office supplies
    "5360",                                         # Telephone/internet
    "5370",                                         # Insurance
    "5380",                                         # Professional/legal
    "5390",                                         # Bank charges
    "5391",                                         # Travel
    "5392",                                         # Training
    "5393",                                         # Repairs & maintenance
    "5394",                                         # Printing
    "5395",                                         # Subscriptions
    "5399",                                         # Miscellaneous

    # Depreciation
    "5400", "5410", "5420", "5430", "5440", "5460",

    # Finance Costs
    "5600", "5610", "5620", "5630", "5640",

    # Tax Expense
    "5700", "5710", "5720",
})


# ─────────────────────────────────────────────────────────────────────────────
# INDUSTRY PACKS: additional codes added on top of CORE
# ─────────────────────────────────────────────────────────────────────────────

# Retail, Wholesale, Food & Beverage — physical goods, POS, stock management
_PACK_TRADING: frozenset[str] = frozenset({
    # Inventory
    "1140", "1141", "1143", "1144", "1145", "1146",
    # Sales Revenue
    "4100", "4110", "4130", "4140",
    # Cost of Sales
    "5100", "5110", "5160",
    # Selling & Distribution
    "5200", "5210", "5220", "5230", "5240",
    # ECL on receivables
    "5500", "5520",
})

# Manufacturing — raw materials, WIP, production overhead
_PACK_MANUFACTURING: frozenset[str] = frozenset({
    # Inventory (all types)
    "1140", "1141", "1142", "1143", "1144", "1145", "1146",
    # Capital WIP (for plant under construction)
    "1218",
    # Sales Revenue (manufactured goods)
    "4100", "4110", "4130",
    # Cost of Sales — full manufacturing set
    "5100", "5110", "5120", "5130", "5140", "5150", "5160",
    # Selling
    "5200", "5210", "5220", "5230",
    # Depreciation — machinery
    "5420",
    # Impairment
    "5500", "5510", "5520", "5530",
})

# Professional Services — no inventory, no COGS, service-focused
_PACK_SERVICES: frozenset[str] = frozenset({
    # Service Revenue
    "4100", "4120",
    # Selling (limited — mainly marketing and commissions)
    "5200", "5210", "5220",
    # ECL on receivables
    "5500", "5520",
    # Intangibles (software licenses, IP)
    "1230", "1233", "1234",
})

# Construction — contracts, materials, subcontractors
_PACK_CONSTRUCTION: frozenset[str] = frozenset({
    # Materials inventory
    "1140", "1141", "1145",
    # Capital WIP (projects)
    "1218",
    # Contract/Sales Revenue
    "4100", "4110", "4120",
    # Deferred Revenue (stage billing)
    "2141",
    # Cost of Sales — contract costs
    "5100", "5110", "5130", "5150", "5160",
    # Project expenses
    "5200", "5220", "5230",
    # Provisions (warranties / claims)
    "2230", "2232", "2233",
    # Impairment
    "5500", "5530",
})

# Healthcare — services + medical supplies/equipment
_PACK_HEALTHCARE: frozenset[str] = frozenset({
    # Service/consultation revenue
    "4100", "4120",
    # Medical supplies (treated as inventory)
    "1140", "1144",
    # Cost of supplies
    "5100", "5110",
    # Selling (limited)
    "5200", "5210",
    # Donations/grants received
    "4260",
    # Impairment + ECL
    "5500", "5520",
    # Medical equipment — additional PPE
    "1216",
    # Provisions (malpractice)
    "2230", "2233",
})

# Technology / Software — SaaS, IT services, licenses
_PACK_TECHNOLOGY: frozenset[str] = frozenset({
    # Service/subscription revenue
    "4100", "4120",
    # Deferred Revenue (annual subscriptions)
    "2141",
    # Intangibles — software, IP
    "1230", "1231", "1233", "1234",
    # ROU (server rooms / office leases)
    "1240", "1241", "1243",
    "2220", "2221", "2222",
    # Selling & Marketing
    "5200", "5210", "5220",
    # ECL on receivables
    "5500", "5520",
    # Vehicle expense (minimal)
    "5397",
})

# Agriculture — crops, livestock, land
_PACK_AGRICULTURE: frozenset[str] = frozenset({
    # Produce and livestock inventory
    "1140", "1141", "1143", "1144", "1145",
    # Land (significant asset)
    "1211",
    # Agricultural produce sales
    "4100", "4110",
    # Cost of produce
    "5100", "5110", "5130", "5150", "5160",
    # Selling
    "5200", "5220", "5230",
    # Vehicle & equipment running costs
    "5393", "5397",
    # Impairment (biological assets)
    "5500", "5530",
})

# Hospitality — hotels, restaurants, events
_PACK_HOSPITALITY: frozenset[str] = frozenset({
    # F&B inventory
    "1140", "1141", "1144",
    # Revenue (room, F&B, events)
    "4100", "4110", "4120", "4230",
    # Cost of F&B
    "5100", "5110", "5160",
    # Selling & Guest services
    "5200", "5210", "5220", "5230", "5240",
    # Leases (building/equipment)
    "1240", "1241", "1243",
    "2220", "2221", "2222",
    # ECL
    "5500", "5520",
    # Donations/events
    "5396",
})

# Transport & Logistics — freight, fleet, warehousing
_PACK_TRANSPORT: frozenset[str] = frozenset({
    # Freight/transport revenue
    "4100", "4110", "4120",
    # Spare parts / consumables (light inventory)
    "1154",
    # Vehicle assets
    "1214",
    "1223",                                         # Accum. Dep. — Vehicles
    "5430",                                         # Depreciation — Vehicles
    # Fleet running costs
    "5393", "5397",
    # Selling
    "5200", "5220", "5230",
    # Leases (vehicles / warehouses)
    "1240", "1242", "1243",
    "2220", "2221", "2222",
    # ECL
    "5500", "5520",
})


# ─────────────────────────────────────────────────────────────────────────────
# Master mapping: industry name → full account code set (CORE + PACK)
# ─────────────────────────────────────────────────────────────────────────────
INDUSTRY_ACCOUNTS: dict[str, frozenset[str] | None] = {
    "Retail":                  CORE_ACCOUNTS | _PACK_TRADING,
    "Wholesale":               CORE_ACCOUNTS | _PACK_TRADING,
    "Food & Beverage":         CORE_ACCOUNTS | _PACK_TRADING | frozenset({"1141","1145"}),
    "Manufacturing":           CORE_ACCOUNTS | _PACK_MANUFACTURING,
    "Professional Services":   CORE_ACCOUNTS | _PACK_SERVICES,
    "Healthcare":              CORE_ACCOUNTS | _PACK_HEALTHCARE,
    "Technology":              CORE_ACCOUNTS | _PACK_TECHNOLOGY,
    "Construction":            CORE_ACCOUNTS | _PACK_CONSTRUCTION,
    "Agriculture":             CORE_ACCOUNTS | _PACK_AGRICULTURE,
    "Hospitality":             CORE_ACCOUNTS | _PACK_HOSPITALITY,
    "Transport & Logistics":   CORE_ACCOUNTS | _PACK_TRANSPORT,
    "Other":                   None,  # None = full IFRS catalogue (all 182)
}


def get_industry_codes(industry: str | None) -> frozenset[str] | None:
    """
    Return the set of account codes for a given industry.
    Returns None if the full catalogue should be used (Other / unknown).
    """
    if not industry:
        return None
    return INDUSTRY_ACCOUNTS.get(industry)


def get_industry_label(industry: str | None) -> str:
    """Human-readable description of what the pack includes."""
    descriptions = {
        "Retail":                "Core + Merchandise Inventory + COGS + Selling",
        "Wholesale":             "Core + Merchandise Inventory + COGS + Selling",
        "Food & Beverage":       "Core + Perishable Inventory + COGS + Selling",
        "Manufacturing":         "Core + Raw Materials / WIP / FG + Production Costs",
        "Professional Services": "Core + Service Revenue + Intangibles",
        "Healthcare":            "Core + Service Revenue + Medical Supplies",
        "Technology":            "Core + Service/Subscription Revenue + Intangibles + Leases",
        "Construction":          "Core + Contract Revenue + Materials + Provisions",
        "Agriculture":           "Core + Produce Inventory + Agricultural Costs",
        "Hospitality":           "Core + F&B Inventory + Room/F&B Revenue",
        "Transport & Logistics": "Core + Freight Revenue + Fleet Assets",
        "Other":                 "Full IFRS Catalogue (all 182 accounts)",
    }
    return descriptions.get(industry or "Other", "Full IFRS Catalogue")
