<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\TeknisiController;
use App\Http\Controllers\PekerjaanController;
use App\Http\Controllers\KasbonController;
use App\Http\Controllers\PenggajianController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\AkunTeknisiController;
use App\Http\Controllers\AbsensiController;
use App\Http\Controllers\PenugasanController;
use App\Http\Controllers\KelolaAdminController;
use Illuminate\Support\Facades\Route;


Route::get('/', function () {
    return view('welcome');
});

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // ===== KELOLA TEKNISI =====
    Route::prefix('teknisi')->name('teknisi.')->group(function () {
        Route::get('/', [TeknisiController::class, 'index'])->name('index');
        Route::post('/', [TeknisiController::class, 'store'])->name('store');
        Route::get('/{id}', [TeknisiController::class, 'show'])->name('show');
        Route::put('/{id}', [TeknisiController::class, 'update'])->name('update');
        Route::delete('/{id}', [TeknisiController::class, 'destroy'])->name('destroy');
        
        // TAMBAHKAN INI - Redirect ke route absensi yang benar
        Route::get('/absensi', function() {
            return redirect()->route('absensi.index');
        })->name('absensi');
    });
    
    // ===== AKUN TEKNISI =====
    Route::prefix('akun-teknisi')->name('akun-teknisi.')->group(function () {
        Route::get('/', [AkunTeknisiController::class, 'index'])->name('index');
        Route::post('/', [AkunTeknisiController::class, 'store'])->name('store');
        Route::get('/{id}', [AkunTeknisiController::class, 'show'])->name('show');
        Route::get('/{id}/edit', [AkunTeknisiController::class, 'edit'])->name('edit');
        Route::put('/{id}', [AkunTeknisiController::class, 'update'])->name('update');
        Route::delete('/{id}', [AkunTeknisiController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/update-status', [AkunTeknisiController::class, 'updateStatus'])->name('update-status');
    });
    
    // ===== ABSENSI =====
    Route::prefix('absensi')->name('absensi.')->group(function () {
        Route::get('/', [AbsensiController::class, 'index'])->name('index');
        Route::get('/{id}', [AbsensiController::class, 'show'])->name('show');
        Route::put('/{id}', [AbsensiController::class, 'update'])->name('update');
    });
    
    // ===== KELOLA PEKERJAAN =====
    Route::prefix('pekerjaan')->name('pekerjaan.')->group(function () {
        // Data Penugasan dengan CRUD lengkap
        Route::prefix('penugasan')->name('penugasan.')->group(function () {
            
             // ✅ TAMBAHKAN INI - Route untuk ambil tarif
                Route::get('/tarif-by-kategori', [PenugasanController::class, 'getTarifByKategori'])
              ->name('getTarifByKategori');
              

             Route::get('/get-teknisi', [PenugasanController::class, 'getTeknisiByDate'])->name('get-teknisi');
             Route::get('/', [PenugasanController::class, 'index'])->name('index');
            Route::post('/', [PenugasanController::class, 'store'])->name('store');
            Route::get('/{id}', [PenugasanController::class, 'show'])->name('show');
            Route::get('/{id}/edit', [PenugasanController::class, 'edit'])->name('edit');
            Route::put('/{id}', [PenugasanController::class, 'update'])->name('update');
            Route::delete('/{id}', [PenugasanController::class, 'destroy'])->name('destroy');
            Route::post('/delete-multiple', [PenugasanController::class, 'destroyMultiple'])->name('delete-multiple');
        });
        
    // Submenu: Monitoring Progres
    Route::get('/monitoring', [PenugasanController::class, 'monitoring'])->name('monitoring');
});
    
    // ===== GAJI =====
    Route::prefix('gaji')->group(function () {
        Route::get('/perhitungan', fn() => redirect()->route('penggajian.index'))->name('gaji.perhitungan');
        Route::get('/kasbon', fn() => redirect()->route('kasbon.index'))->name('gaji.kasbon');
    });
    
    Route::post('/penggajian/hitung', [PenggajianController::class, 'hitungGaji'])->name('penggajian.hitung');
    Route::post('/penggajian/bayar-semua', [PenggajianController::class, 'prosesSemuaPembayaran'])->name('penggajian.bayar-semua');
    Route::post('/penggajian/{penggajian}/bayar', [PenggajianController::class, 'prosesPembayaran'])->name('penggajian.bayar');
    Route::get('/penggajian/export', [PenggajianController::class, 'export'])->name('penggajian.export');
    Route::get('/penggajian/{penggajian}/slip', [PenggajianController::class, 'slip'])->name('penggajian.slip');
    Route::get('/penggajian/{penggajian}/detail', [PenggajianController::class, 'detail'])->name('penggajian.detail');
    Route::post('/penggajian/{penggajian}/recalculate', [PenggajianController::class, 'recalculate'])
     ->name('penggajian.recalculate');
    Route::post('/penggajian/{penggajian}/update-kasbon', [PenggajianController::class, 'updateKasbon'])
     ->name('penggajian.update-kasbon');
    Route::post('/penggajian/{penggajian}/update-makan', [PenggajianController::class, 'updateMakan'])
     ->name('penggajian.update-makan');
    Route::resource('penggajian', PenggajianController::class);

    Route::get('/kasbon/statistics', [KasbonController::class, 'statistics'])->name('kasbon.statistics');
Route::post('/kasbon/{id}/lunas', [KasbonController::class, 'markAsLunas'])->name('kasbon.lunas');
Route::resource('kasbon', KasbonController::class);
    
    // ===== LAPORAN =====
    Route::prefix('laporan')->name('laporan.')->group(function () {
        Route::get('/', [LaporanController::class, 'index'])->name('index');
        Route::get('/statistics', [LaporanController::class, 'statistics'])->name('statistics');
        Route::get('/kasbon', [LaporanController::class, 'kasbon'])->name('kasbon');
        Route::get('/teknisi', [LaporanController::class, 'teknisi'])->name('teknisi');
        Route::get('/absensi', [LaporanController::class, 'absensi'])->name('absensi');
        Route::get('/pekerjaan', [LaporanController::class, 'pekerjaan'])->name('pekerjaan');
        Route::get('/penggajian', [LaporanController::class, 'penggajian'])->name('penggajian');
        Route::get('/data-teknisi', [LaporanController::class, 'dataTeknisi'])->name('data_teknisi');
        Route::get('/export', [LaporanController::class, 'export'])->name('export');
        Route::get('/kasbon/export', [LaporanController::class, 'exportKasbon'])->name('kasbon.export');
        Route::get('/teknisi/export', [LaporanController::class, 'exportTeknisi'])->name('teknisi.export');
        Route::get('/absensi/export', [LaporanController::class, 'exportAbsensi'])->name('absensi.export');
        Route::get('/pekerjaan/export', [LaporanController::class, 'exportPekerjaan'])->name('pekerjaan.export');
        Route::get('/penggajian/export', [LaporanController::class, 'exportPenggajian'])->name('penggajian.export');
        Route::get('/data-teknisi/export', [LaporanController::class, 'exportDataTeknisi'])->name('data_teknisi.export');
    });
    
    // ===== KELOLA ADMIN =====
    // Route::resource('kelola-admin', KelolaAdminController::class);

    // ===== PROFILE =====
    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');
    });
});

require __DIR__.'/auth.php';

// Helper route to create storage link on hosting
Route::get('/create-storage-link', function () {
    try {
        \Illuminate\Support\Facades\Artisan::call('storage:link');
        return 'Storage link created successfully!';
    } catch (\Exception $e) {
        return 'Error: ' . $e->getMessage();
    }
});

Route::get('/clear-cache', function () {
    try {
        \Illuminate\Support\Facades\Artisan::call('config:clear');
        \Illuminate\Support\Facades\Artisan::call('cache:clear');
        \Illuminate\Support\Facades\Artisan::call('view:clear');
        \Illuminate\Support\Facades\Artisan::call('route:clear');
        return 'All cache cleared successfully!';
    } catch (\Exception $e) {
        return 'Error: ' . $e->getMessage();
    }
});

