<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
    DashboardController,
    MemberController,
    PackageController,
    ExpenseController,
    ReportController,
    ProfileController,
    BinController,
    ProgressController,
    SubmissionController,
    FormController,
    SmsController,
    OpenRegistrationController,
    PaymentController
};

Route::get('/', fn() => view('welcome'));

// Public application form
Route::get('/application-form/{submission?}', [FormController::class, 'openForm'])->name('form.open');
Route::post('/application-form-submit', [FormController::class, 'submitForm'])->name('form.submit');

// Open registration
Route::get('/register', [OpenRegistrationController::class, 'create'])->name('open-registration.create');
Route::post('/register', [OpenRegistrationController::class, 'store'])->name('open-registration.store');

Route::middleware(['auth', 'verified'])->group(function () {

    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::resource('members', MemberController::class);
    Route::get('/members/filter/{date}', [MemberController::class, 'filterByDate'])->name('members.filter');
    Route::get('/members/{id}/view', [MemberController::class, 'view'])->name('members.view');

    Route::resource('packages', PackageController::class);
    Route::resource('expenses', ExpenseController::class);

    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index');
        Route::get('/today', [ReportController::class, 'generateTodayReport'])->name('today');
        Route::get('/monthly', [ReportController::class, 'generateMonthlyReport'])->name('monthly');
        Route::get('/{report}', [ReportController::class, 'show'])->name('show');
        Route::get('/{report}/download', [ReportController::class, 'download'])->name('download');
    });

    Route::prefix('submissions')->name('submissions.')->group(function () {
        Route::get('/', [SubmissionController::class, 'index'])->name('index');
        Route::get('/bin', [SubmissionController::class, 'bin'])->name('bin');
        Route::get('/open', [SubmissionController::class, 'create'])->name('open');
        Route::post('/store', [SubmissionController::class, 'store'])->name('store');

        Route::get('/check-expiries', [SubmissionController::class, 'checkExpiries'])->name('checkExpiries');
        Route::get('/send-birthday-wishes', [SubmissionController::class, 'sendBirthdayWishes'])->name('sendBirthdayWishes');

        Route::get('/{submission}', [SubmissionController::class, 'show'])->name('show');
        Route::post('/{submission}/approve', [SubmissionController::class, 'approve'])->name('approve');
        Route::post('/{submission}/reject', [SubmissionController::class, 'reject'])->name('reject');
        Route::post('/{submission}/restore', [SubmissionController::class, 'restore'])->name('restore');
        Route::post('/{submission}/admission-status', [SubmissionController::class, 'updateAdmissionStatus'])->name('admissionStatus');
        Route::delete('/{submission}/destroy', [SubmissionController::class, 'destroy'])->name('destroy');
    });

    Route::get('/track-application', [SubmissionController::class, 'trackForm'])->name('submissions.track.form');
    Route::post('/track-application', [SubmissionController::class, 'track'])->name('submissions.track');

    Route::prefix('progress')->name('progress.')->group(function () {
        Route::get('/', [ProgressController::class, 'index'])->name('index');
        Route::post('/store', [ProgressController::class, 'store'])->name('store');
        Route::get('/{member}', [ProgressController::class, 'show'])->name('show');
        Route::get('/{member}/compare', [ProgressController::class, 'compare'])->name('compare');
        Route::get('/{member}/detail/{date}', [ProgressController::class, 'detail'])->name('detail');
        Route::get('/{member}/download/{date}', [ProgressController::class, 'download'])->name('download');
        Route::get('/{member}/download-month/{month}', [ProgressController::class, 'downloadMonth'])->name('downloadMonth');
    });

    Route::prefix('bin')->name('bin.')->group(function () {
        Route::get('/', [BinController::class, 'index'])->name('index');
        Route::delete('/{id}/destroy', [BinController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/restore', [BinController::class, 'restore'])->name('restore');
    });

    Route::prefix('sms')->name('sms.')->group(function () {
        Route::get('/', [SmsController::class, 'index'])->name('index');
        Route::post('/send', [SmsController::class, 'send'])->name('send');
    });

    Route::get('/sms-logs', [SmsController::class, 'logs'])->name('sms.logs');

    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [ProfileController::class, 'edit'])->name('edit');
        Route::patch('/', [ProfileController::class, 'update'])->name('update');
        Route::delete('/', [ProfileController::class, 'destroy'])->name('destroy');
    });
Route::get('/track-application', [SubmissionController::class, 'trackForm'])->name('track.form');
Route::post('/track-application', [SubmissionController::class, 'trackPublic'])->name('track.search');


    Route::get('/payments', [PaymentController::class, 'index'])->name('payments.index');
    Route::get('/payments/expiring', [PaymentController::class, 'expiring'])->name('payments.expiring');
    Route::get('/payments/{member}/history', [PaymentController::class, 'history'])->name('payments.history');
    Route::post('/payments/{member}/renew', [PaymentController::class, 'renewAjax'])->name('payments.renew');
    Route::delete('/payments/{payment}', [PaymentController::class, 'destroy'])->name('payments.destroy');
    Route::get('/payments/sms-preview', [PaymentController::class, 'smsPreview'])->name('payments.sms-preview');
    Route::post('/payments/send-sms', [PaymentController::class, 'sendSmsBulk'])->name('payments.send-sms');
});

require __DIR__ . '/auth.php';