<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DepartemenController;
use App\Http\Controllers\JabatanController;
use App\Http\Controllers\KaryawanController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\UserController;


Route::get('/', function () {

    if (Auth::check()) {

        if (Auth::user()->role == 'administrator') {
            return redirect()->route('administrator.dashboard');
        } elseif (Auth::user()->role == 'atasan') {
            return redirect()->route('dashboard.index');
        } elseif (Auth::user()->role == 'anak_asuh') {
            return redirect()->route('hasilPenilaian.index');
        }
    }

    return redirect()->route('login');
});

Route::middleware('guest')->group(function () {

    Route::get('login', [AuthController::class, 'showLoginForm'])
        ->name('login');

    Route::post('login', [AuthController::class, 'login'])
        ->middleware('throttle:5,1');
});

Route::middleware('auth')->group(function () {

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

    Route::middleware('role:administrator,atasan')->group(function () {

        Route::get('laporan/data', [LaporanController::class, 'getData'])
            ->name('laporan.getData');

        Route::get('laporan', [LaporanController::class, 'index'])
            ->name('laporan.index');

        Route::get('laporan/create', [LaporanController::class, 'create'])
            ->name('laporan.create');

        Route::get('laporanv2/create', [LaporanController::class, 'createv2'])
            ->name('laporanv2.create');

        Route::post('laporan', [LaporanController::class, 'store'])
            ->name('laporan.store');

        Route::post('laporanv2', [LaporanController::class, 'storev2'])
            ->name('laporanv2.store');

        Route::get('laporan/export', [LaporanController::class, 'export'])
            ->name('laporan.export');
    });


    Route::middleware('role:administrator,atasan,anak_asuh')->group(function () {

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

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

        Route::get(
            '/dashboard/monitoring/data/{karyawan:slug?}',
            [DashboardController::class, 'dashboardData']
        )->name('dashboard.monitoring.data');

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

        Route::put(
            'laporan/{laporan}/konfirmasi',
            [LaporanController::class, 'storeKonfirmasi']
        )->name('laporan.konfirmasi');

        Route::get('laporan/{laporan}', [LaporanController::class, 'show'])
            ->name('laporan.show');

        Route::get('/profile/edit', [KaryawanController::class, 'editProfile'])
            ->name('profile.edit');

        Route::put('/profile/update', [KaryawanController::class, 'updateProfile'])
            ->name('profile.update');

        Route::get('/password/edit', [UserController::class, 'editPassword'])
            ->name('password.edit');

        Route::put('/password/update', [UserController::class, 'changePassword'])
            ->name('password.update');

        Route::get('/hasil-penilaian/data', [DashboardController::class, 'hasilPenilaianData'])
            ->name('hasilPenilaian.data');

        Route::get('/hasil-penilaian', [DashboardController::class, 'hasilPenilaian'])
            ->name('hasilPenilaian.index');
    });

    Route::middleware('role:administrator')->group(function () {

  Route::get('user/export', [UserController::class, 'export'])
            ->name('user.export');
        // Dashboard
        Route::get(
            '/administrator/dashboard/monitoring',
            [DashboardController::class, 'administratorDashboard']
        )->name('administrator.monitoring');

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

        Route::get(
            '/administrator/dashboard/{karyawan:slug}',
            [DashboardController::class, 'dashboard']
        )->name('administratorViewAtasan.dashboard');

        // ATR
        Route::post(
            '/karyawan/{id}/atr',
            [KaryawanController::class, 'storeAtr']
        );

        // Karyawan
        Route::get('karyawan/data', [KaryawanController::class, 'getData'])
            ->name('karyawan.getData');

        Route::resource('karyawan', KaryawanController::class);

        // User
        Route::get('user/data', [UserController::class, 'getData'])
            ->name('user.getData');

        Route::resource('user', UserController::class);

        // Jabatan
        Route::get('jabatan/data', [JabatanController::class, 'getData'])
            ->name('jabatan.getData');

        Route::get('jabatan', [JabatanController::class, 'index'])
            ->name('jabatan.index');

        Route::post('jabatan', [JabatanController::class, 'store'])
            ->name('jabatan.store');

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

        Route::put('jabatan/{jabatan}', [JabatanController::class, 'update'])
            ->name('jabatan.update');

        Route::delete('jabatan/{id}', [JabatanController::class, 'destroy'])
            ->name('jabatan.destroy');

        // Departemen
        Route::get('departemen/data', [DepartemenController::class, 'getData'])
            ->name('departemen.getData');

        Route::get('departemen', [DepartemenController::class, 'index'])
            ->name('departemen.index');

        Route::post('departemen', [DepartemenController::class, 'store'])
            ->name('departemen.store');

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

        Route::put('departemen/{departemen}', [DepartemenController::class, 'update'])
            ->name('departemen.update');

        Route::delete('departemen/{id}', [DepartemenController::class, 'destroy'])
            ->name('departemen.destroy');
    });
});
