"""Generate Accounting submenu page.tsx files from nav map."""
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1] / "frontend" / "app" / "accounting"

# (relative_path_without_page, kind, kwargs)
# kinds: biz, blocked, cheques, cheques_new, receipts, transfers, recon,
#        items, warehouses, redirect, reexport

PAGES = [
    # treasury
    ("treasury/receipts-payments", "receipts", {}),
    ("treasury/cash-boxes", "redirect", {"to": "/accounting/treasury"}),
    ("treasury/banks", "redirect", {"to": "/accounting/treasury"}),
    ("treasury/transfers", "transfers", {}),
    ("treasury/reconciliation", "recon", {}),
    ("treasury/cheques", "cheques", {}),
    ("treasury/cheques/new", "cheques_new", {}),
    ("treasury/guarantees", "biz", {"title": "ضمانت‌ها", "module": "treasury", "docType": "guarantee"}),
    ("treasury/facilities", "biz", {"title": "تسهیلات و اعتبارات", "module": "treasury", "docType": "facility"}),
    # purchase
    ("purchase/requests", "biz", {"title": "درخواست خرید", "module": "purchase", "docType": "request"}),
    ("purchase/orders", "biz", {"title": "سفارش خرید", "module": "purchase", "docType": "order"}),
    ("purchase/goods-receipts", "biz", {"title": "رسید کالا", "module": "purchase", "docType": "goods_receipt"}),
    ("purchase/invoices", "biz", {"title": "فاکتور خرید", "module": "purchase", "docType": "invoice"}),
    ("purchase/returns", "biz", {"title": "مرجوعی خرید", "module": "purchase", "docType": "return"}),
    ("purchase/prepayments", "biz", {"title": "پیش‌پرداخت‌ها", "module": "purchase", "docType": "prepayment"}),
    ("purchase/settlements", "biz", {"title": "تسویه با تأمین‌کننده", "module": "purchase", "docType": "settlement"}),
    # sales
    ("sales/opportunities", "biz", {"title": "فرصت‌های فروش", "module": "sales", "docType": "opportunity"}),
    ("sales/proformas", "biz", {"title": "پیش‌فاکتور", "module": "sales", "docType": "proforma"}),
    ("sales/orders", "biz", {"title": "سفارش فروش", "module": "sales", "docType": "order"}),
    ("sales/invoices", "biz", {"title": "فاکتور فروش", "module": "sales", "docType": "invoice"}),
    ("sales/returns", "biz", {"title": "مرجوعی فروش", "module": "sales", "docType": "return"}),
    ("sales/pre-receipts", "biz", {"title": "پیش‌دریافت‌ها", "module": "sales", "docType": "pre_receipt"}),
    ("sales/settlements", "biz", {"title": "تسویه حساب مشتریان", "module": "sales", "docType": "settlement"}),
    # vouchers extras
    ("vouchers/recurring", "biz", {"title": "اسناد تکراری", "module": "vouchers", "docType": "recurring"}),
    ("vouchers/adjustments", "biz", {"title": "اسناد اصلاحی", "module": "vouchers", "docType": "adjustment"}),
    ("vouchers/templates", "biz", {"title": "الگوی سند", "module": "vouchers", "docType": "template"}),
    # payroll
    ("payroll/employees", "biz", {"title": "کارمندان", "module": "payroll", "docType": "employee"}),
    ("payroll/contracts", "biz", {"title": "تعریف قرارداد", "module": "payroll", "docType": "contract"}),
    ("payroll/structures", "biz", {"title": "ساختار حقوق", "module": "payroll", "docType": "structure"}),
    ("payroll/items", "biz", {"title": "اقلام حقوق و مزایا", "module": "payroll", "docType": "pay_item"}),
    ("payroll/calculate", "redirect", {"to": "/accounting/payroll"}),
    ("payroll/payments", "biz", {"title": "پرداخت حقوق", "module": "payroll", "docType": "payment"}),
    ("payroll/allocation", "biz", {"title": "تسهیم هزینه حقوق", "module": "payroll", "docType": "allocation"}),
    ("payroll/reports", "biz", {"title": "گزارش‌های حقوق", "module": "payroll", "docType": "report"}),
    # assets
    ("assets/dashboard", "redirect", {"to": "/accounting/assets"}),
    ("assets/depreciate", "redirect", {"to": "/accounting/assets"}),
    ("assets/schedule", "biz", {"title": "برنامه استهلاک", "module": "assets", "docType": "schedule"}),
    ("assets/transfers", "biz", {"title": "انتقال دارایی", "module": "assets", "docType": "transfer"}),
    ("assets/accumulated", "biz", {"title": "استهلاک انباشته", "module": "assets", "docType": "accumulated"}),
    ("assets/valuation", "biz", {"title": "ارزش‌گذاری دارایی", "module": "assets", "docType": "valuation"}),
    ("assets/dispose", "biz", {"title": "معرفی و فروش دارایی", "module": "assets", "docType": "dispose"}),
    # inventory
    ("inventory/items", "items", {}),
    ("inventory/movements", "biz", {"title": "گردش کالا", "module": "inventory", "docType": "movement"}),
    ("inventory/valuation", "biz", {"title": "ارزش‌گذاری موجودی", "module": "inventory", "docType": "valuation"}),
    ("inventory/settings", "warehouses", {}),
    ("inventory/transfers", "biz", {"title": "انتقال انبار", "module": "inventory", "docType": "transfer"}),
    ("inventory/receipts-issues", "biz", {"title": "رسید ورود و خروج", "module": "inventory", "docType": "receipt_issue"}),
    ("inventory/adjustments", "biz", {"title": "تعدیل موجودی", "module": "inventory", "docType": "adjustment"}),
    ("inventory/reports", "biz", {"title": "گزارش‌های انبار", "module": "inventory", "docType": "report"}),
    # budget
    ("budget/definitions", "biz", {"title": "تعریف بودجه", "module": "budget", "docType": "definition"}),
    ("budget/operational", "biz", {"title": "بودجه‌ریزی عملیاتی", "module": "budget", "docType": "operational"}),
    ("budget/control", "biz", {"title": "کنترل بودجه", "module": "budget", "docType": "control"}),
    ("budget/variance", "biz", {"title": "تحلیل انحراف بودجه", "module": "budget", "docType": "variance"}),
    ("budget/performance", "biz", {"title": "مقایسه عملکرد", "module": "budget", "docType": "performance"}),
    ("budget/forecast", "biz", {"title": "پیش‌بینی مالی", "module": "budget", "docType": "forecast"}),
    ("budget/scenarios", "biz", {"title": "سناریوهای مالی", "module": "budget", "docType": "scenario"}),
    ("budget/reports", "biz", {"title": "گزارش بودجه", "module": "budget", "docType": "report"}),
    # compliance
    ("compliance/policies", "biz", {"title": "سیاست‌ها و قوانین", "module": "compliance", "docType": "policy"}),
    ("compliance/approvals", "biz", {"title": "گردش تأیید", "module": "compliance", "docType": "approval"}),
    ("compliance/sod", "biz", {"title": "تفکیک وظایف (SoD)", "module": "compliance", "docType": "sod"}),
    ("compliance/controls", "biz", {"title": "کنترل‌های داخلی", "module": "compliance", "docType": "control"}),
    ("compliance/risks", "biz", {"title": "مدیریت ریسک", "module": "compliance", "docType": "risk"}),
    ("compliance/records", "biz", {"title": "اسناد و سوابق", "module": "compliance", "docType": "record"}),
    ("compliance/violations", "biz", {"title": "گزارش تخلفات", "module": "compliance", "docType": "violation"}),
    # AI blocked
    ("ai", "blocked", {"title": "دستیار حسابداری"}),
    ("ai/qa", "blocked", {"title": "پرسش و پاسخ"}),
    ("ai/analytics", "blocked", {"title": "تحلیل هوشمند"}),
    ("ai/forecasts", "blocked", {"title": "پیش‌بینی‌ها"}),
    ("ai/anomalies", "blocked", {"title": "کشف ناهنجاری"}),
    ("ai/reconciliation", "blocked", {"title": "تطبیق هوشمند"}),
    ("ai/documents", "blocked", {"title": "تحلیل اسناد"}),
    ("ai/recommendations", "blocked", {"title": "پیشنهادهای هوشمند"}),
    # settings
    ("settings/company", "biz", {"title": "اطلاعات شرکت", "module": "settings", "docType": "company"}),
    ("settings/tax", "biz", {"title": "تنظیمات مالیاتی", "module": "settings", "docType": "tax"}),
    ("settings/general", "biz", {"title": "تنظیمات عمومی", "module": "settings", "docType": "general"}),
    ("settings/backup", "biz", {"title": "پشتیبان‌گیری", "module": "settings", "docType": "backup"}),
    # integration
    ("integration/bank", "biz", {"title": "اتصال بانک", "module": "integration", "docType": "bank"}),
    ("integration/gateway", "biz", {"title": "درگاه پرداخت", "module": "integration", "docType": "gateway"}),
    ("integration/crm", "biz", {"title": "اتصال CRM", "module": "integration", "docType": "crm"}),
    ("integration/inventory", "biz", {"title": "اتصال انبار", "module": "integration", "docType": "inventory"}),
    ("integration/payroll", "biz", {"title": "اتصال حقوق", "module": "integration", "docType": "payroll"}),
    ("integration/webhooks", "biz", {"title": "وب‌سرویس‌ها", "module": "integration", "docType": "webhook"}),
    ("integration/import-export", "biz", {"title": "ورود / خروج داده", "module": "integration", "docType": "import_export"}),
    ("integration/reports", "biz", {"title": "گزارش یکپارچگی", "module": "integration", "docType": "report"}),
    # documents
    ("documents/financial", "biz", {"title": "اسناد مالی", "module": "documents", "docType": "financial"}),
    ("documents/purchase", "biz", {"title": "اسناد خرید", "module": "documents", "docType": "purchase"}),
    ("documents/sales", "biz", {"title": "اسناد فروش", "module": "documents", "docType": "sales"}),
    ("documents/hr", "biz", {"title": "اسناد پرسنلی", "module": "documents", "docType": "hr"}),
    ("documents/categories", "biz", {"title": "دسته‌بندی اسناد", "module": "documents", "docType": "category"}),
    ("documents/search", "biz", {"title": "جستجوی اسناد", "module": "documents", "docType": "search"}),
    ("documents/versions", "biz", {"title": "نسخه‌های سند", "module": "documents", "docType": "version"}),
    ("documents/retention", "biz", {"title": "نگهداری اسناد", "module": "documents", "docType": "retention"}),
    # monitoring
    ("monitoring", "biz", {"title": "داشبورد نظارتی", "module": "monitoring", "docType": "dashboard"}),
    ("monitoring/recent", "biz", {"title": "عملیات اخیر", "module": "monitoring", "docType": "recent"}),
    ("monitoring/alerts", "biz", {"title": "هشدارها", "module": "monitoring", "docType": "alert"}),
    ("monitoring/activity", "biz", {"title": "فعالیت کاربران", "module": "monitoring", "docType": "activity"}),
    ("monitoring/health", "biz", {"title": "وضعیت سرویس", "module": "monitoring", "docType": "health"}),
    ("monitoring/kpis", "biz", {"title": "شاخص‌های کلیدی", "module": "monitoring", "docType": "kpi"}),
    ("monitoring/performance", "biz", {"title": "عملکرد سیستم", "module": "monitoring", "docType": "performance"}),
]


def render(kind: str, kw: dict) -> str:
    if kind == "biz":
        return f'''"use client";

import {{ BusinessDocsPage }} from "@/components/accounting/BusinessDocsPage";

export default function Page() {{
  return (
    <BusinessDocsPage
      title="{kw["title"]}"
      module="{kw["module"]}"
      docType="{kw["docType"]}"
    />
  );
}}
'''
    if kind == "blocked":
        return f'''"use client";

import {{ BlockedModulePage }} from "@/components/accounting/BusinessDocsPage";

export default function Page() {{
  return <BlockedModulePage title="{kw["title"]}" />;
}}
'''
    if kind == "cheques":
        return '''"use client";

import { ChequesPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <ChequesPage />;
}
'''
    if kind == "cheques_new":
        return '''"use client";

import { ChequesPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <ChequesPage defaultIncoming />;
}
'''
    if kind == "receipts":
        return '''"use client";

import { ReceiptsPaymentsPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <ReceiptsPaymentsPage />;
}
'''
    if kind == "transfers":
        return '''"use client";

import { TransfersPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <TransfersPage />;
}
'''
    if kind == "recon":
        return '''"use client";

import { ReconciliationPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <ReconciliationPage />;
}
'''
    if kind == "items":
        return '''"use client";

import { InventoryItemsPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <InventoryItemsPage />;
}
'''
    if kind == "warehouses":
        return '''"use client";

import { WarehousesPage } from "@/components/accounting/BusinessDocsPage";

export default function Page() {
  return <WarehousesPage />;
}
'''
    if kind == "redirect":
        return f'''import {{ redirect }} from "next/navigation";

export default function Page() {{
  redirect("{kw["to"]}");
}}
'''
    raise ValueError(kind)


def main() -> None:
    created = 0
    for rel, kind, kw in PAGES:
        path = ROOT / rel / "page.tsx"
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(render(kind, kw), encoding="utf-8")
        created += 1
        print(path.relative_to(ROOT.parent.parent))
    print(f"Created/updated {created} pages")


if __name__ == "__main__":
    main()
