from django.urls import path
from django.views.generic import RedirectView
from . import views
from . import sasapay_views
from . import sms_views
from . import developer_payment_views

app_name = 'payments'

urlpatterns = [
    # -----------------------------------------------------------------------
    # Compatibility redirects (referenced in navigation but live in loans app)
    # -----------------------------------------------------------------------
    path('record-payment/', RedirectView.as_view(pattern_name='loans:new_repayment', permanent=False), name='record_payment'),
    path('history/', RedirectView.as_view(pattern_name='loans:repayments', permanent=False), name='payment_history'),

    # -----------------------------------------------------------------------
    # SMS Logs — admin view of every outgoing SMS
    # -----------------------------------------------------------------------
    path('sms/', sms_views.sms_logs, name='sms_logs'),
    path('sms/<uuid:log_id>/', sms_views.sms_log_detail, name='sms_log_detail'),
    path('sms/<uuid:log_id>/resend/', sms_views.resend_sms, name='resend_sms'),
    path('sms/stats/', sms_views.sms_stats_api, name='sms_stats_api'),

    # -----------------------------------------------------------------------
    # M-Pesa (Safaricom) callback URLs
    # -----------------------------------------------------------------------
    path('mpesa/validation/', views.MpesaValidationView.as_view(), name='mpesa_validation'),
    path('mpesa/confirmation/', views.MpesaConfirmationView.as_view(), name='mpesa_confirmation'),
    path('callback/validation/', views.MpesaValidationView.as_view(), name='callback_validation'),
    path('callback/confirmation/', views.MpesaConfirmationView.as_view(), name='callback_confirmation'),

    # -----------------------------------------------------------------------
    # SasaPay callback URLs (no auth — called by SasaPay servers)
    # -----------------------------------------------------------------------
    path('sasapay/ipn/', sasapay_views.SasaPayIPNView.as_view(), name='sasapay_ipn'),
    path('sasapay/stk-callback/', sasapay_views.SasaPaySTKCallbackView.as_view(), name='sasapay_stk_callback'),
    path('sasapay/disbursement-callback/', sasapay_views.SasaPayDisbursementCallbackView.as_view(), name='sasapay_disbursement_callback'),

    # -----------------------------------------------------------------------
    # SasaPay staff initiation endpoints
    # -----------------------------------------------------------------------
    path('sasapay/initiate-stk/', sasapay_views.initiate_stk_view, name='sasapay_initiate_stk'),
    path('sasapay/disburse/', sasapay_views.initiate_disbursement_view, name='sasapay_disburse'),
    path('sasapay/test-token/', sasapay_views.test_sasapay_token_view, name='sasapay_test_token'),
    path('sasapay/test-sms/', sasapay_views.test_sms_view, name='sasapay_test_sms'),

    # -----------------------------------------------------------------------
    # SasaPay dashboard & management
    # -----------------------------------------------------------------------
    path('sasapay/', sasapay_views.sasapay_dashboard, name='sasapay_dashboard'),
    path('sasapay/unknown-payments/', sasapay_views.sasapay_unknown_payments, name='sasapay_unknown_payments'),
    path('sasapay/unknown-payments/<uuid:payment_id>/resolve/', sasapay_views.resolve_unknown_payment, name='sasapay_resolve_unknown'),

    # -----------------------------------------------------------------------
    # M-Pesa admin dashboard and management
    # -----------------------------------------------------------------------
    path('dashboard/', views.mpesa_dashboard, name='dashboard'),
    path('transactions/', views.mpesa_transactions, name='transactions'),
    path('transactions/<uuid:transaction_id>/', views.mpesa_transaction_detail, name='transaction_detail'),
    path('transactions/<uuid:transaction_id>/reprocess/', views.reprocess_transaction, name='reprocess_transaction'),
    path('transactions/<uuid:transaction_id>/manual-match/', views.manual_match_payment, name='manual_match_payment'),
    path('callbacks/', views.mpesa_callbacks, name='callbacks'),

    # Configuration
    path('configuration/', views.mpesa_configuration, name='configuration'),
    path('test-payment/', views.test_mpesa_payment, name='test_payment'),

    # Unconfirmed payments (admin only)
    path('unconfirmed-payments/', views.unconfirmed_payments, name='unconfirmed_payments'),
    path('unconfirmed-payments/<uuid:payment_id>/', views.unconfirmed_payment_detail, name='unconfirmed_payment_detail'),
    path('unconfirmed-payments/<uuid:payment_id>/approve/', views.approve_unconfirmed_payment, name='approve_unconfirmed_payment'),
    path('unconfirmed-payments/<uuid:payment_id>/reject/', views.reject_unconfirmed_payment, name='reject_unconfirmed_payment'),

    # Borrower views
    path('my-payments/', views.my_payments, name='my_payments'),

    # -----------------------------------------------------------------------
    # Developer Payments (PhinTech Solutions Company Ltd — LipiaOnline)
    # -----------------------------------------------------------------------
    path('developer/', developer_payment_views.developer_payments_dashboard, name='developer_payments'),
    path('developer/initiate/', developer_payment_views.initiate_developer_payment_view, name='initiate_developer_payment'),
    path('developer/<uuid:payment_id>/', developer_payment_views.developer_payment_detail, name='developer_payment_detail'),
    path('developer/<uuid:payment_id>/retry/', developer_payment_views.retry_developer_payment, name='retry_developer_payment'),
    path('developer/<uuid:payment_id>/cancel/', developer_payment_views.cancel_developer_payment, name='cancel_developer_payment'),
    path('developer/status/<uuid:payment_id>/', developer_payment_views.check_payment_status, name='developer_payment_status'),
    path('developer/callback/lipia/', developer_payment_views.lipia_online_callback, name='lipia_online_callback'),
]