from django.urls import include, path
from controllers import ChitController, PaymentEditController, TableOrderController
from .views import admin_views as admin_views
from .views import staff_views as staff_views
from . import views
from controllers.Admin import AuthController, StaffController, AddLoanController,LoanleadController,AdminloanregisterController,AdminloanreportController,SetattendanceController,AdminattendanceController,AdminpaymentinvoiceController,AdminoverdueController,ReportsController
from controllers.Staff import OverdueController, Staff_authController,Staff_addloanController,LoanregisterController,LoanreportController,AttendanceController,PaymentinvoiceController
from controllers import LoanEditController
from .middleware import auth_required, guest_only,staff_auth_required, staff_guest_only


def middleware(*decorators):
    """Laravel-style group: apply the given middleware to every route in the list."""
    def group(routes):
        grouped = []
        for r in routes:
            callback = r.callback
            for d in reversed(decorators):
                callback = d(callback)
            grouped.append(path(str(r.pattern), callback, name=r.name))
        return grouped
    return group

admin_guest_urls = middleware(guest_only)([
    path('admin_login/', admin_views.login, name='admin_login'),
])

# ADMIN URLS
admin_open_urls = [
    # path('', admin_views.login, name='login'),
    path('login_validate/', AuthController.login_validate, name='login_validate'),
    path('forgot_password/', admin_views.forgot_password, name='forgot_password'),
    path('new_password/', admin_views.new_password, name='new_password'),
    path('reset_password_validate/', AuthController.reset_password_validate, name='reset_password_validate'),
    ]
admin_auth_urls = middleware(auth_required)([
   
    path('dashboard/', admin_views.dashboard, name='dashboard'),
    path('reset_password/', admin_views.reset_password, name='reset_password'),
    path('staff_collection_data/', StaffController.staff_collection_data, name='staff_collection_data'),
    path('admin_logout/', AuthController.admin_logout, name='admin_logout'),
    # Customer Management
    path('customer_management/', admin_views.customer_management, name='customer_management'),
    path('view_all_customer/', AddLoanController.view_all_customer, name='view_all_customer'),
    path('edit_customer/<int:customer_id>/', AddLoanController.update_customer, name='edit_customer'),
    path('payment_history/', admin_views.payment_history, name='payment_history'),
    path('delete_customer/',AddLoanController.delete_customer,name='delete_customer'),

    # Loan Management
    path('loan_dashboard/', admin_views.loan_dashboard, name='loan_dashboard'),
    path('add_loan/', admin_views.add_loan, name='add_loan'),
    path('addloan/',AddLoanController.addloan,name='addloan'),
    path('get_loan_customer_details/',AddLoanController.get_loan_customer_details,name='get_loan_customer_details'),
    path('loan_reports/', admin_views.loan_reports, name='loan_reports'),
    path('loan_reports/<int:place_id>/', admin_views.loan_reports, name='loan_reports'),
    path('view_loan_report/', admin_views.view_loan_report, name='view_loan_report'),
    path('loan_registers/', admin_views.loan_registers, name='loan_registers'),
    path('loan_entries/', admin_views.loan_entries, name='loan_entries'),
    path('view_loan_entry/', admin_views.view_loan_entry, name='view_loan_entry'),


    # new route changing in admin AdminloanregisterController.py file full route
    path('admin_view_loan_request_page',AdminloanregisterController.admin_view_loan_request_page,name='admin_view_loan_request_page'),
    path('admin_add_place',AdminloanregisterController.admin_add_place,name='admin_add_place'),
    path('admin_update_place',AdminloanregisterController.admin_update_place,name='admin_update_place'),
    path('admin_place_completed_loan_data',AdminloanregisterController.admin_place_completed_loan_data,name='admin_place_completed_loan_data'),
    path('admin_loan_entries_data',AdminloanregisterController.admin_loan_entries_data,name='admin_loan_entries_data'),
    path('admin_completed_loan_data',AdminloanregisterController.admin_completed_loan_data,name='admin_completed_loan_data'),
    path('admin_completed_loan_export/', AdminloanregisterController.admin_completed_loan_export, name='completed_loan_export'),

# new route changing in admin AdminloanreportController.py file full route
    path('loan_reports_data/', AdminloanreportController.admin_loan_reports_data, name='loan_reports_data'),
    path('loan_reports_export/', AdminloanreportController.admin_loan_reports_export, name='loan_reports_export'),
    path('record_payment/', AdminloanreportController.admin_record_payment, name='record_payment'),
    path('paid_due_history/', AdminloanreportController.admin_paid_due_history, name='paid_due_history'),
    path('paid_due_history_data/', AdminloanreportController.admin_paid_due_history_data, name='paid_due_history_data'),
    path('paid_due_history_export/', AdminloanreportController.admin_paid_due_history_export, name='paid_due_history_export'),
    path('today_collections_data/', AdminloanreportController.admin_today_collections_data, name='today_collections_data'),
    path('collection_list_data/', AdminloanreportController.admin_collection_list_data, name='collection_list_data'),
    path('collections_export/', AdminloanreportController.admin_collections_export, name='collections_export'),
    path('loan_reports_summary/', AdminloanreportController.admin_loan_reports_summary, name='loan_reports_summary'),
    path('payment_edit_requests/', PaymentEditController.payment_edit_requests, name='payment_edit_requests'),
    path('payment_edit_requests_data/', PaymentEditController.payment_edit_requests_data, name='payment_edit_requests_data'),
    path('payment_edit_requests_count/', PaymentEditController.payment_edit_requests_count, name='payment_edit_requests_count'),
    path('review_payment_edit/', PaymentEditController.review_payment_edit, name='review_payment_edit'),
    path('table_order/', TableOrderController.table_order, name='table_order'),
    path('loan_period_report/', AdminloanreportController.admin_loan_period_report, name='loan_period_report'),

    path('reports/', admin_views.reports, name='reports'),
    path('reports_data/', ReportsController.reports_data, name='reports_data'),
    path('reports_export/', ReportsController.reports_export, name='reports_export'),

    path('view_customer_details/', admin_views.view_customer_details, name='view_customer_details'),
    path('view_customer_details/<int:loan_id>/', admin_views.view_customer_details, name='view_customer_details'),
    path('completed_loan/', admin_views.completed_loan, name='completed_loan'),
    path('view_completed_loan/', admin_views.view_completed_loan, name='view_completed_loan'),
    path('place_completed_loan/', admin_views.place_completed_loan, name='place_completed_loan'),
    path('place_completed_loan/<int:place_id>/', admin_views.place_completed_loan, name='place_completed_loan'),
    path('view_place_loan/', admin_views.view_place_loan, name='view_place_loan'),
    path('view_place_loan/<int:loan_id>/', admin_views.view_place_loan, name='view_place_loan'),
  
    path('overdue_loan/', admin_views.overdue_loan, name='overdue_loan'),
    path('overdue_loan_data/', AdminoverdueController.admin_overdue_loan_data, name='overdue_loan_data'),
    path('overdue_loan_export/', AdminoverdueController.admin_overdue_loan_export, name='overdue_loan_export'),
    path('view_overdue_loan/', AdminoverdueController.admin_view_overdue_loan, name='view_overdue_loan'),

    path('today_collections/', admin_views.today_collections, name='today_collections'),
    path('collection_list/', admin_views.collection_list, name='collection_list'),
    path('edit_loan/', LoanEditController.admin_edit_loan, name='edit_loan'),

    # Staff Management
    path('add_staff/', admin_views.add_staff, name='add_staff'),
    path('storestaff/', StaffController.storestaff, name='storestaff'),
    path('delete_staff/', StaffController.delete_staff, name='delete_staff'),
    path('view_staff/', StaffController.view_staff, name='view_staff'), 
    path('staff_list/', admin_views.staff_list, name='staff_list'),
    path('edit_staff/<int:staff_id>/', admin_views.edit_staff, name='edit_staff'),
    path('update_staff/<int:staff_id>/', StaffController.update_staff, name='update_staff'),
    path('update_staff_status/', StaffController.update_staff_status, name='update_staff_status'),
    path('view_staff_password/', StaffController.view_staff_password, name='view_staff_password'),
    path('staff_collection_details/', admin_views.staff_collection_details, name='staff_collection_details'),
    path('staff_attendence/', admin_views.staff_attendence, name='staff_attendence'),
    path('attendance_data/', AdminattendanceController.admin_attendance_data, name='attendance_data'),
    path('attendance_export/', AdminattendanceController.admin_attendance_export, name='attendance_export'),
    path('set_attendance/', admin_views.set_attendance, name='set_attendance'),
    path('store_attendance_master/', SetattendanceController.store_attendance_master, name='store_attendance_master'),

    # Loan Leads
    path('loan_request/', LoanleadController.loan_request, name='loan_request'),
    path('add_lead/', admin_views.add_lead, name='add_lead'),
    path('store_leads/', LoanleadController.store_leads, name='store_leads'),
    path('get_loan_requests/', LoanleadController.get_loan_requests, name='get_loan_requests'),
    path('confirm_lead/', LoanleadController.confirm_lead, name='confirm_lead'),
    path('loan_request_count/', LoanleadController.loan_request_count, name='loan_request_count'),
    path('view_loan_request/<int:lead_id>/', LoanleadController.view_loan_request, name='view_loan_request'),


    path('payment_invoice/', AdminpaymentinvoiceController.payment_invoice, name='payment_invoice'),
    path('payment_invoice_data/', AdminpaymentinvoiceController.payment_invoice_data, name='payment_invoice_data'),
])

# MONTHLY CHIT FUND (admin: full control)
admin_chit_urls = middleware(auth_required)([
    path('monthlychitfund/', ChitController.chit_list, name='chit_list'),
    path('monthlychitfund/new/', ChitController.chit_form, name='chit_new'),
    path('monthlychitfund/<int:chit_id>/edit/', ChitController.chit_form, name='chit_edit'),
    path('monthlychitfund/<int:chit_id>/', ChitController.chit_detail, name='chit_view'),
    path('chit_list_data/', ChitController.chit_list_data, name='chit_list_data'),
    path('chit_list_export/', ChitController.chit_list_export, name='chit_list_export'),
    path('chit_save/', ChitController.chit_save, name='chit_save'),
    path('chit_delete/', ChitController.chit_delete, name='chit_delete'),
    path('chit_detail_data/', ChitController.chit_detail_data, name='chit_detail_data'),
    path('chit_member_save/', ChitController.chit_member_save, name='chit_member_save'),
    path('chit_member_delete/', ChitController.chit_member_delete, name='chit_member_delete'),
    path('chit_term_save/', ChitController.chit_term_save, name='chit_term_save'),
    path('chit_auction_preview/', ChitController.chit_auction_preview, name='chit_auction_preview'),
    path('chit_pay/', ChitController.chit_pay, name='chit_pay'),
    path('chit_payment_delete/', ChitController.chit_payment_delete, name='chit_payment_delete'),
    path('chit_reports/', ChitController.chit_reports, name='chit_reports'),
    path('chit_reports_data/', ChitController.chit_reports_data, name='chit_reports_data'),
    path('chit_reports_export/', ChitController.chit_reports_export, name='chit_reports_export'),
])

admin_urls = admin_guest_urls+ admin_open_urls + admin_auth_urls + admin_chit_urls

# STAFF URLS

staff_guest_urls = middleware(staff_guest_only)([
    path('login/', staff_views.login, name='staff_login'),
])

staff_open_urls = [
    path('login_validate/', Staff_authController.staff_login_validate, name='staff_login_validate'),
    path('forgot_password/', staff_views.forgot_password, name='forgot_password'),
    path('new_password/', staff_views.new_password, name='new_password'),
    path('staff_reset_password/', Staff_authController.staff_reset_password, name='staff_reset_password'),
]

staff_auth_urls = middleware(staff_auth_required)([
    path('dashboard/', staff_views.dashboard, name='dashboard'),
    path('reset_password/', staff_views.reset_password, name='reset_password'),
    path('staff_logout/', Staff_authController.staff_logout, name='staff_logout'),
    # Customer Management
    path('customer_management/', staff_views.customer_management, name='customer_management'),
    path('payment_history/', staff_views.payment_history, name='payment_history'),
    path('edit_customer/<int:customer_id>/', Staff_addloanController.staff_update_customer, name='edit_customer'),


    # checkin
    path('staff_checkin/',AttendanceController.staff_checkin,name='staff_checkin'),
    path('staff_checkout/', AttendanceController.staff_checkout, name='staff_checkout'),


    # Loan Management
    path('add_loan/', staff_views.add_loan, name='add_loan'),
    path('staff_addloan/',Staff_addloanController.staff_addloan,name='staff_addloan'),
    path('staff_get_loan_customer_details/',Staff_addloanController.staff_get_loan_customer_details,name='staff_get_loan_customer_details'),
    path('staff_view_all_customer/',Staff_addloanController.staff_view_all_customer,name='staff_view_all_customer'),

    path('loan_management/', staff_views.loan_management, name='loan_management'),
    path('loan_registers/', staff_views.loan_registers, name='loan_registers'),
    path('loan_entries_data/', LoanregisterController.loan_entries_data, name='loan_entries_data'),
    path('completed_loan_data/', LoanregisterController.completed_loan_data, name='completed_loan_data'),
    path('completed_loan_export/', LoanregisterController.completed_loan_export, name='completed_loan_export'),

    path('view_loan_request_page/', LoanregisterController.view_loan_request_page, name='view_loan_request_page'),
    path('loan_reports_data/', LoanreportController.loan_reports_data, name='loan_reports_data'),
    path('loan_reports_export/', LoanreportController.loan_reports_export, name='loan_reports_export'),
    path('record_payment/', LoanreportController.record_payment, name='record_payment'),
    path('loan_reports_summary/', LoanreportController.loan_reports_summary, name='loan_reports_summary'),
    path('request_payment_edit/', PaymentEditController.request_payment_edit, name='request_payment_edit'),
    path('table_order/', TableOrderController.table_order, name='table_order'),
    path('loan_period_report/', LoanreportController.loan_period_report, name='loan_period_report'),

    # dashboard page
    path('today_collections_data/', LoanreportController.today_collections_data, name='today_collections_data'),

    path('paid_due_history/', LoanreportController.paid_due_history, name='paid_due_history'),
    path('paid_due_history_data/', LoanreportController.paid_due_history_data, name='paid_due_history_data'),
    path('paid_due_history_export/', LoanreportController.paid_due_history_export, name='paid_due_history_export'),
    path('collection_list_data/', LoanreportController.collection_list_data, name='collection_list_data'),
    path('collections_export/', LoanreportController.collections_export, name='collections_export'),
    path('place_completed_loan_data/', LoanregisterController.place_completed_loan_data, name='place_completed_loan_data'),

    path('add_place/', LoanregisterController.add_place, name='add_place'),
    path('update_place/', LoanregisterController.update_place, name='update_place'),


    path('loan_reports/<int:place_id>/', staff_views.loan_reports, name='loan_reports'),
    path('place_completed_loan/<int:place_id>/', staff_views.place_completed_loan, name='place_completed_loan'),

    path('view_place_loan/<int:loan_id>/', staff_views.view_place_loan, name='view_place_loan'),

    path('loan_entries/', staff_views.loan_entries, name='loan_entries'),
    path('view_loan_entry/', staff_views.view_loan_entry, name='view_loan_entry'),
    path('view_loan_report/', staff_views.view_loan_report, name='view_loan_report'),
    path('view_customer_details/', staff_views.view_customer_details, name='view_customer_details'),
    path('completed_loan/', staff_views.completed_loan, name='completed_loan'),
    path('view_completed_loan/', staff_views.view_completed_loan, name='view_completed_loan'),

    path('overdue_loan/', staff_views.overdue_loan, name='overdue_loan'),
    path('overdue_loan_data/', OverdueController.staff_overdue_loan_data, name='overdue_loan_data'),
    path('overdue_loan_export/', OverdueController.staff_overdue_loan_export, name='overdue_loan_export'),
    path('view_overdue_loan/', OverdueController.staff_view_overdue_loan, name='view_overdue_loan'),
    path('edit_loan/', LoanEditController.staff_edit_loan, name='edit_loan'),
    path('today_collections/', staff_views.today_collections, name='today_collections'),
    path('collection_list/', staff_views.collection_list, name='collection_list'),

    # Attendance
    path('attendence/', staff_views.attendence, name='attendence'),

    path('payment_invoice/', PaymentinvoiceController.payment_invoice, name='payment_invoice'),
    path('staff_payment_invoice_data/', PaymentinvoiceController.staff_payment_invoice_data, name='staff_payment_invoice_data'),

])


# MONTHLY CHIT FUND (staff: chits of their places, view + collect)
staff_chit_urls = middleware(staff_auth_required)([
    path('monthlychitfund/', ChitController.chit_list, name='chit_list'),
    path('monthlychitfund/<int:chit_id>/', ChitController.chit_detail, name='chit_view'),
    path('chit_list_data/', ChitController.chit_list_data, name='chit_list_data'),
    path('chit_list_export/', ChitController.chit_list_export, name='chit_list_export'),
    path('chit_detail_data/', ChitController.chit_detail_data, name='chit_detail_data'),
    path('chit_pay/', ChitController.chit_pay, name='chit_pay'),
    path('chit_reports/', ChitController.chit_reports, name='chit_reports'),
    path('chit_reports_data/', ChitController.chit_reports_data, name='chit_reports_data'),
    path('chit_reports_export/', ChitController.chit_reports_export, name='chit_reports_export'),
])

staff_urls = staff_guest_urls + staff_open_urls + staff_auth_urls + staff_chit_urls


# AddLoanController calls reverse()/redirect() with plain names (no "admin:" prefix),
# so these aliases give it the names it expects. Same URLs as the admin routes above.
controller_alias_urls = [
    path('admin/customer_management/', auth_required(admin_views.customer_management), name='customer_management'),
    path('admin/edit_customer/<int:customer_id>/', auth_required(AddLoanController.update_customer), name='edit_customer'),
    path('admin/view_customer_details/<int:loan_id>/', auth_required(admin_views.view_customer_details), name='view_customerDetails'),
]

urlpatterns = [
    path('admin/', include((admin_urls, 'admin'), namespace='admin')),
    path('staff/', include((staff_urls, 'staff'), namespace='staff')),
] + controller_alias_urls