from django.urls import include, path
from rest_framework.routers import DefaultRouter

from apps.reporting.views import (
    EnterpriseReportRunView,
    ReportAsyncExportView,
    ReportCatalogView,
    ReportFavoritesAndRecentsView,
    ReportHistoryView,
    ReportJobStatusDownloadView,
    ReportSavedPresetViewSet,
    ReportsDashboardView,
    ScheduledReportViewSet,
)

router = DefaultRouter()
router.register("presets", ReportSavedPresetViewSet, basename="report-presets")
router.register("schedules", ScheduledReportViewSet, basename="report-schedules")

# Map hyphenated Section 20 URLs directly to canonical report codes
ENDPOINT_ALIASES = [
    ("trial-balance", "trial_balance"),
    ("general-ledger", "general_ledger"),
    ("profit-loss", "profit_loss"),
    ("balance-sheet", "balance_sheet"),
    ("cash-flow", "cash_flow"),
    ("voucher-register", "voucher_register"),
    ("construction-wip", "construction_wip"),
    ("boq-performance", "boq_execution"),
    ("project-profitability", "project_profitability"),
    ("cost-to-complete", "cost_to_complete"),
    ("ar-aging", "ar_aging"),
    ("ap-aging", "ap_aging"),
    ("cost-centres", "cost_centres"),
]

urlpatterns = [
    path("", include(router.urls)),
    path("dashboard/", ReportsDashboardView.as_view()),
    path("catalog/", ReportCatalogView.as_view()),
    path("favorites/", ReportFavoritesAndRecentsView.as_view(), {"kind": "favorites"}),
    path("recent/", ReportFavoritesAndRecentsView.as_view(), {"kind": "recent"}),
    path("history/", ReportHistoryView.as_view()),
    path("jobs/<uuid:job_id>/status/", ReportJobStatusDownloadView.as_view(), {"mode": "status"}),
    path("jobs/<uuid:job_id>/download/", ReportJobStatusDownloadView.as_view(), {"mode": "download"}),
]

for slug, code in ENDPOINT_ALIASES:
    urlpatterns += [
        path(f"{slug}/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "json"}),
        path(f"{slug}/export/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "export_xlsx"}),
        path(f"{slug}/export-pdf/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "export_pdf"}),
        path(f"{slug}/export-xlsx/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "export_xlsx"}),
        path(f"{slug}/export-csv/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "export_csv"}),
        path(f"{slug}/preview/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "preview"}),
        path(f"{slug}/print/", EnterpriseReportRunView.as_view(), {"report_code": code, "action_mode": "print"}),
    ]

# Dynamic generic runner supporting all 51 report codes cleanly
urlpatterns += [
    path("run/<str:report_code>/", EnterpriseReportRunView.as_view(), {"action_mode": "json"}),
    path("<str:report_code>/preview/", EnterpriseReportRunView.as_view(), {"action_mode": "preview"}),
    path("<str:report_code>/print/", EnterpriseReportRunView.as_view(), {"action_mode": "print"}),
    path("<str:report_code>/export-pdf/", EnterpriseReportRunView.as_view(), {"action_mode": "export_pdf"}),
    path("<str:report_code>/export-xlsx/", EnterpriseReportRunView.as_view(), {"action_mode": "export_xlsx"}),
    path("<str:report_code>/export-csv/", EnterpriseReportRunView.as_view(), {"action_mode": "export_csv"}),
    path("<str:report_code>/export-async/", ReportAsyncExportView.as_view()),
]

