<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\LoginController;
use App\Http\Controllers\Admin\ForgotPasswordController;
use App\Http\Controllers\UnitAdmin\UnitAdminController;
use App\Http\Controllers\UnitAdmin\LoginController as UnitAdminLoginController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\UnitAdmin\UnitAdminReportController;

Route::get('/test-mail', function () {
    \Mail::raw('Test Mailtrap Working', function ($message) {
        $message->to('test@example.com')
                ->subject('Test');
    });

    return 'Mail Sent';
});

// Redirect root to admin login
Route::get('/', function () {
    return redirect()->route('admin.login');
});
Route::get('/login', function () {
    return redirect()->route('admin.login');
})->name('login');

// ─────────────────────────────────
// ADMIN LOGIN (public)
// ─────────────────────────────────
Route::get('/admin/login', [LoginController::class, 'showLogin'])->name('admin.login');
Route::post('/admin/login', [LoginController::class, 'login'])->name('admin.login.post');

// ─────────────────────────────────
// UNIT ADMIN LOGIN (public)
// ─────────────────────────────────
Route::get('/unit-admin/login', [UnitAdminLoginController::class, 'showLogin'])->name('unit-admin.login');
Route::post('/unit-admin/login', [UnitAdminLoginController::class, 'login'])->name('unit-admin.login.post');

// ─────────────────────────────────
// PASSWORD RESET (public) — MUST be before prefix groups
// ─────────────────────────────────
Route::get('/admin/forgot-password', [ForgotPasswordController::class, 'showForgotForm'])->name('admin.password.forgot');
Route::post('/admin/forgot-password', [ForgotPasswordController::class, 'sendResetLink'])->name('admin.password.forgot.post');
Route::get('/admin/reset-password', [ForgotPasswordController::class, 'showResetForm'])->name('admin.password.reset.form');
Route::post('/admin/reset-password', [ForgotPasswordController::class, 'resetPassword'])->name('admin.password.reset.post');

Route::get('/unit-admin/forgot-password', [ForgotPasswordController::class, 'showForgotForm'])->name('unit-admin.password.forgot');
Route::post('/unit-admin/forgot-password', [ForgotPasswordController::class, 'sendResetLink'])->name('unit-admin.password.forgot.post');
Route::get('/unit-admin/reset-password', [ForgotPasswordController::class, 'showResetForm'])->name('unit-admin.password.reset.form');
Route::post('/unit-admin/reset-password', [ForgotPasswordController::class, 'resetPassword'])->name('unit-admin.password.reset.post');


// ── MEMBER PASSWORD RESET PAGE (for email link) ──
Route::get('/reset-password', function () {
    return view('member.reset-password', [
        'token' => request('token'),
        'email' => request('email'),
    ]);
})->name('member.password.reset');


// ─────────────────────────────────
// PROTECTED ADMIN ROUTES
// ─────────────────────────────────
Route::prefix('admin')
    ->name('admin.')
    ->middleware('auth')
    ->group(function () {
        Route::get('/reports', [ReportController::class, 'index'])->name('reports');
        Route::get('/reports/download', [ReportController::class, 'download'])->name('reports.download');
        Route::get('/reports/preview', [ReportController::class, 'preview'])->name('reports.preview');
        Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
        Route::get('/states', [AdminController::class, 'states'])->name('states');
        Route::post('/states', [AdminController::class, 'storeState'])->name('states.store');
        Route::put('/states', [AdminController::class, 'updateState'])->name('states.update');
        Route::delete('/states', [AdminController::class, 'deleteState'])->name('states.delete');

        Route::get('/districts', [AdminController::class, 'districts'])->name('districts');
        Route::post('/districts', [AdminController::class, 'storeDistrict'])->name('districts.store');
        Route::put('/districts', [AdminController::class, 'updateDistrict'])->name('districts.update');
        Route::delete('/districts', [AdminController::class, 'deleteDistrict'])->name('districts.delete');

        Route::get('/units', [AdminController::class, 'units'])->name('units');
        Route::post('/units', [AdminController::class, 'storeUnit'])->name('units.store');
        Route::put('/units', [AdminController::class, 'updateUnit'])->name('units.update');
        Route::delete('/units', [AdminController::class, 'deleteUnit'])->name('units.delete');

        Route::get('/unit-admins', [AdminController::class, 'unitAdmins'])->name('unit-admins');
        Route::post('/unit-admins', [AdminController::class, 'storeUnitAdmin'])->name('unit-admins.store');
        Route::post('/unit-admins/update', [AdminController::class, 'updateUnitAdmin'])->name('unit-admins.update');
        Route::post('/unit-admins/reset-password', [AdminController::class, 'resetUnitAdminPassword'])->name('unit-admins.reset-password');
        Route::post('/unit-admins/toggle-status', [AdminController::class, 'toggleUnitAdminStatus'])->name('unit-admins.toggle-status');
        Route::delete('/unit-admins', [AdminController::class, 'deleteUnitAdmin'])->name('unit-admins.delete');

        Route::get('/members', [AdminController::class, 'members'])->name('members');
        Route::post('/members', [AdminController::class, 'storeMember'])->name('members.store');
        Route::get('/members/{id}/detail', [AdminController::class, 'memberDetail'])->name('members.detail');
        Route::delete('/members', [AdminController::class, 'deleteMember'])->name('members.delete');
        Route::post('/members/approve', [AdminController::class, 'approveMember'])->name('members.approve');
        Route::post('/members/reject', [AdminController::class, 'rejectMember'])->name('members.reject');


        Route::get('/plans', [AdminController::class, 'plans'])->name('plans');
        Route::post('/plans', [AdminController::class, 'storePlan'])->name('plans.store');
        Route::post('/plans/update', [AdminController::class, 'updatePlan'])->name('plans.update');
        Route::post('/plans/toggle-status', [AdminController::class, 'togglePlanStatus'])->name('plans.toggle-status');
        Route::delete('/plans', [AdminController::class, 'deletePlan'])->name('plans.delete');

        Route::get('/news', [AdminController::class, 'news'])->name('news');
        Route::post('/news', [AdminController::class, 'storeNews'])->name('news.store');
        Route::post('/news/update', [AdminController::class, 'updateNews'])->name('news.update');
        Route::post('/news/toggle-status', [AdminController::class, 'toggleNewsStatus'])->name('news.toggle-status');
        Route::delete('/news', [AdminController::class, 'deleteNews'])->name('news.delete');

        Route::get('/events', [AdminController::class, 'events'])->name('events');
        Route::post('/events', [AdminController::class, 'storeEvent'])->name('events.store');
        Route::post('/events/update', [AdminController::class, 'updateEvent'])->name('events.update');
        Route::post('/events/status', [AdminController::class, 'updateEventStatus'])->name('events.status');
        Route::delete('/events', [AdminController::class, 'deleteEvent'])->name('events.delete');
        Route::get('/events/{id}/detail', [AdminController::class, 'eventDetail'])->name('events.detail');
        Route::get('/events/{id}/registrations', [AdminController::class, 'eventRegistrations'])->name('events.registrations');

        Route::get('/change-password', [AdminController::class, 'showChangePassword'])->name('change-password');
        Route::post('/change-password', [AdminController::class, 'changePassword'])->name('change-password.post');

        Route::get('/logout', [AdminController::class, 'logout'])->name('logout');

        // ── BULK IMPORT ──
        Route::get(
            '/bulk-import',
            [AdminController::class, 'bulkImport']
        )
            ->name('bulk-import');
        Route::get(
            '/bulk-import/template',
            [AdminController::class, 'downloadTemplate']
        )
            ->name('bulk-import.template');
        Route::post(
            '/bulk-import',
            [AdminController::class, 'processBulkImport']
        )
            ->name('bulk-import.process');
    });

// ─────────────────────────────────
// PROTECTED UNIT ADMIN ROUTES
// ─────────────────────────────────
Route::prefix('unit-admin')
    ->name('unit-admin.')
    ->middleware(['unit_admin'])
    ->group(function () {
        Route::get('/reports', [UnitAdminReportController::class, 'index'])->name('reports');
        Route::get('/reports/preview', [UnitAdminReportController::class, 'preview'])->name('reports.preview');
        Route::get('/reports/download', [UnitAdminReportController::class, 'download'])->name('reports.download');


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

        Route::get('/members', [UnitAdminController::class, 'members'])->name('members');
        Route::post('/members', [UnitAdminController::class, 'storeMember'])->name('members.store');
        Route::get('/members/{id}/detail', [UnitAdminController::class, 'memberDetail'])->name('members.detail');
        Route::post('/members/approve', [UnitAdminController::class, 'approveMember'])->name('members.approve');
        Route::post('/members/reject', [UnitAdminController::class, 'rejectMember'])->name('members.reject');
        Route::post('/members/import-csv', [UnitAdminController::class, 'importMembersCsv'])->name('members.import-csv');
        Route::get('/members/csv-template', [UnitAdminController::class, 'downloadCsvTemplate'])->name('members.csv-template');

        // ── NEWS (unit admin can create/manage) ──
        Route::get('/news', [UnitAdminController::class, 'news'])->name('news');
        Route::post('/news', [UnitAdminController::class, 'storeNews'])->name('news.store');
        Route::post('/news/update', [UnitAdminController::class, 'updateNews'])->name('news.update');
        Route::post('/news/toggle-status', [UnitAdminController::class, 'toggleNewsStatus'])->name('news.toggle-status');
        Route::delete('/news', [UnitAdminController::class, 'deleteNews'])->name('news.delete');

        // ── EVENTS (unit admin can create/manage) ──
        Route::get('/events', [UnitAdminController::class, 'events'])->name('events');
        Route::post('/events', [UnitAdminController::class, 'storeEvent'])->name('events.store');
        Route::post('/events/update', [UnitAdminController::class, 'updateEvent'])->name('events.update');
        Route::post('/events/status', [UnitAdminController::class, 'updateEventStatus'])->name('events.status');
        Route::delete('/events', [UnitAdminController::class, 'deleteEvent'])->name('events.delete');
        Route::get('/events/{id}/detail', [UnitAdminController::class, 'eventDetail'])->name('events.detail');
        Route::get('/events/{id}/registrations', [UnitAdminController::class, 'eventRegistrations'])->name('events.registrations');


        Route::get('/change-password', [UnitAdminController::class, 'showChangePassword'])->name('change-password');
        Route::post('/change-password', [UnitAdminController::class, 'changePassword'])->name('change-password.post');

        Route::get('/logout', [UnitAdminController::class, 'logout'])->name('logout');

        // ── BULK IMPORT ──
        Route::get(
            '/bulk-import',
            [UnitAdminController::class, 'bulkImport']
        )
            ->name('bulk-import');
        Route::get(
            '/bulk-import/template',
            [UnitAdminController::class, 'downloadTemplate']
        )
            ->name('bulk-import.template');
        Route::post(
            '/bulk-import',
            [UnitAdminController::class, 'processBulkImport']
        )
            ->name('bulk-import.process');
    });


