<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\Admin\StatsController;
use App\Http\Controllers\StaffSurveyController;
use App\Http\Controllers\Admin\SurveyController;
use App\Http\Controllers\Admin\ResponseController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Middleware\RoleMiddleware;
use App\Http\Controllers\Admin\UlasanController;

// login teruz
Route::get('/', fn() => redirect()->route('login'));

// PUBLIC SURVEY ROUTES (Accessible by everyone)
Route::get('survey/{survey:uuid}', [StaffSurveyController::class, 'show'])->name('surveys.show');
Route::post('survey/{survey:uuid}', [StaffSurveyController::class, 'store'])->name('surveys.store');
Route::get('survey-success', function() {return view('staff.success');})->name('surveys.success');

//Login Routes
Route::get('login', [AuthController::class, 'showLogin'])->name('login');
Route::post('login', [AuthController::class, 'doLogin'])->name('login.post');
Route::post('logout', [AuthController::class, 'logout'])->name('logout');

// Route Admin 
Route::prefix('admin')
    ->middleware(['auth', \App\Http\Middleware\RoleMiddleware::class . ':admin'])
    ->as('admin.')
    ->group(function () {

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

        // users
        Route::get('users', [AdminController::class, 'users'])->name('users.index');
        Route::post('users', [AdminController::class, 'storeUser'])->name('users.store');
        Route::put('users/{id}', [AdminController::class, 'updateUser'])->name('users.update');
        Route::delete('users/{id}', [AdminController::class, 'deleteUser'])->name('users.delete');

        //ulasan
        Route::get('/surveys/ulasan-list', [UlasanController::class, 'ulasanPage'])->name('surveys.ulasan');
        Route::get('/surveys/ulasan/export', [UlasanController::class, 'downloadCSV'])->name('surveys.ulasan.export');
        Route::post('/surveys/{id}/ulasan', [UlasanController::class, 'updateUlasan'])->name('surveys.update_ulasan');

        //stats
        Route::get('surveys/{id}/stats', [StatsController::class, 'showStats'])->name('surveys.statistics');
        Route::get('surveys/{id}/stats/print', [StatsController::class, 'printStats'])->name('surveys.statistics_print');

        // borang survey
        Route::resource('surveys', SurveyController::class);

        // display jawapan
        Route::prefix('responses')->name('responses.')->group(function () {
            Route::get('/respondents/{survey}', [ResponseController::class, 'showRespondents'])->name('respondents');
            Route::get('/list', [ResponseController::class, 'list'])->name('list');
            Route::get('/detail/{response}', [ResponseController::class, 'detail'])->name('detail');
        });

    });

