<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserLoginController;
use App\Http\Controllers\ActivityController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\Admin\ActivityController as AdminActivityController;
use App\Http\Controllers\Admin\SuperAdminController;

/*
|--------------------------------------------------------------------------
| ROUTE UTAMA
|--------------------------------------------------------------------------
*/

Route::get('/', function () {
    return redirect()->route('login');
});


/*
|--------------------------------------------------------------------------
| LOGIN & LOGOUT
|--------------------------------------------------------------------------
*/
Route::get('/login-user', function () {
    return view('login-user');
})->name('login');

Route::post('/login-user', [UserLoginController::class, 'login'])
    ->name('login.user');

Route::post('/logout', [UserLoginController::class, 'logout'])
    ->name('logout');


/*
|--------------------------------------------------------------------------
| AUTH AREA (CUSTOM SESSION LOGIN)
|--------------------------------------------------------------------------
*/
Route::middleware(['web'])->group(function () {

    /*
    |--------------------------------------------------------------------------
    | AREA USER (PETUGAS / PENGAWAS)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:user'])->group(function () {

        // Dashboard Utama User
        Route::get('/user/dashboard', [ActivityController::class, 'index'])
            ->name('user.dashboard');

        // Form Input Kegiatan
        Route::get('/activity-input', function () {
            return view('user.input-activity');
        })->name('input.activity');

        // Proses Simpan Kegiatan
        Route::post('/activities/store', [ActivityController::class, 'store'])
            ->name('activities.store');

        // List & Detail Kegiatan
        Route::get('/activity-list', [ActivityController::class, 'list'])
            ->name('activity.list');

        Route::get('/activity-detail/{id}', [ActivityController::class, 'show'])
            ->name('activity.detail');

        // Edit & Update
        Route::get('/activity/{id}/edit', [ActivityController::class, 'edit'])
            ->name('activity.edit');

        Route::post('/activity/{id}/update', [ActivityController::class, 'update'])
            ->name('activity.update');

        // Tandai Selesai
        Route::post('/activity/{id}/selesai', [ActivityController::class, 'selesai'])
            ->name('activity.selesai');
            
        Route::post('/activity/{id}/progress', [ActivityController::class, 'storeProgress'])
            ->name('activity.progress');
    });

    // Settings User (Dapat diakses oleh semua role)
    Route::get('/settings', function () {
        return view('user.settings');
    })->name('settings');

    Route::post('/settings/update', [UserLoginController::class, 'updateSettings'])
        ->name('settings.update');

    // Frontend API Proxy untuk Notifikasi
    Route::get('/user/notifications-api', [NotificationController::class, 'getNotifications']);
    Route::post('/user/notifications-api/read', [NotificationController::class, 'markAsRead']);


    /*
    |--------------------------------------------------------------------------
    | AREA ADMIN (KASI)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:admin'])->group(function () {

        // Dashboard Admin
        Route::get('/admin/dashboard', [ActivityController::class, 'adminDashboard'])
            ->name('admin.dashboard');

        // Monitoring Kegiatan Semua User
        Route::get('/admin/kegiatan', [AdminActivityController::class, 'index'])
            ->name('admin.kegiatan.index');

        Route::get('/admin/kegiatan/{id}', [AdminActivityController::class, 'show'])
            ->name('admin.kegiatan.show');



        // Manajemen User (Petugas)
        Route::get('/admin/users', [UserLoginController::class, 'userList'])
            ->name('admin.users');

        Route::get('/admin/users/create', [UserLoginController::class, 'createUser'])
            ->name('admin.users.create');

        Route::post('/admin/users/store', [UserLoginController::class, 'storeUser'])
            ->name('admin.users.store');

        Route::get('/admin/users/{id}/edit', [UserLoginController::class, 'editUser'])
            ->name('admin.users.edit');

        Route::post('/admin/users/{id}/update', [UserLoginController::class, 'updateUser'])
            ->name('admin.users.update');

        Route::delete('/admin/users/{id}', [UserLoginController::class, 'destroyUser'])
            ->name('admin.users.destroy');
    });


    /*
    |--------------------------------------------------------------------------
    | AREA SUPER ADMIN (KABID)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:superadmin'])->group(function () {

        // Dashboard Utama
        Route::get('/superadmin/dashboard', [SuperAdminController::class, 'index'])
            ->name('admin.superadmin.dashboard');

        // Kegiatan
        Route::get('/superadmin/kegiatan/create', [SuperAdminController::class, 'createKegiatan'])
            ->name('admin.superadmin.kegiatan.create');

        Route::post('/superadmin/kegiatan/store', [SuperAdminController::class, 'storeKegiatan'])
            ->name('admin.superadmin.kegiatan.store');

        Route::get('/superadmin/kegiatan/{id}', [SuperAdminController::class, 'show'])
            ->name('admin.superadmin.kegiatan.show');

        Route::post('/superadmin/kegiatan/{id}/approve', [SuperAdminController::class, 'approve'])
            ->name('admin.superadmin.kegiatan.approve');
            
        Route::post('/superadmin/kegiatan/{id}/revision', [SuperAdminController::class, 'revision'])
            ->name('admin.superadmin.kegiatan.revision');

        Route::delete('/superadmin/kegiatan/{id}', [SuperAdminController::class, 'destroy'])
            ->name('admin.superadmin.kegiatan.destroy');

        // Arsip
        Route::get('/superadmin/arsip', [SuperAdminController::class, 'arsip'])
            ->name('admin.superadmin.arsip');
            
        Route::post('/superadmin/kegiatan/{id}/archive', [SuperAdminController::class, 'archive'])
            ->name('admin.superadmin.kegiatan.archive');

        // Export
        Route::post('/superadmin/unduh-laporan', [SuperAdminController::class, 'export'])
            ->name('admin.superadmin.export');

        // User Management
        Route::get('/superadmin/users', [SuperAdminController::class, 'userIndex'])
            ->name('admin.superadmin.user.index');

        Route::get('/superadmin/users/create', [SuperAdminController::class, 'createUser'])
            ->name('admin.superadmin.user.create');

        Route::post('/superadmin/users/store', [SuperAdminController::class, 'storeUser'])
            ->name('admin.superadmin.user.store');

        Route::get('/superadmin/users/{id}/edit', [SuperAdminController::class, 'editUser'])
            ->name('admin.superadmin.user.edit');

        Route::post('/superadmin/users/{id}/update', [SuperAdminController::class, 'updateUser'])
            ->name('admin.superadmin.user.update');

        Route::delete('/superadmin/users/{id}', [SuperAdminController::class, 'destroyUser'])
            ->name('admin.superadmin.user.destroy');
    });
});
