<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AkunTeknisiController;
use App\Http\Controllers\Api\AbsensiApiController;
use App\Http\Controllers\Api\PenugasanApiController;
use App\Http\Controllers\Api\GajiApiController;
use App\Http\Controllers\Api\KasbonApiController;
use App\Http\Controllers\Api\DashboardApiController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// Routes untuk Teknisi (Mobile App)
Route::prefix('teknisi')->group(function () {
    Route::post('/login', [AkunTeknisiController::class, 'login']);
    
    Route::middleware('auth:api')->group(function () {
        Route::post('/logout', [AkunTeknisiController::class, 'logout']);
        Route::get('/me', [AkunTeknisiController::class, 'me']);
        Route::post('/refresh', [AkunTeknisiController::class, 'refresh']);
        Route::post('/change-password', [AkunTeknisiController::class, 'changePassword']);
    });
});

Route::prefix('absensi')->group(function () {
    Route::post('/absen-masuk', [AbsensiApiController::class, 'absenMasuk']);
    Route::post('/absen-keluar', [AbsensiApiController::class, 'absenKeluar']);
    Route::get('/check-status/{id_teknisi}', [AbsensiApiController::class, 'checkStatus']);

    // ✅ Route spesifik HARUS di atas /{id}
    Route::get('/riwayat', [AbsensiApiController::class, 'riwayat']);
    Route::get('/statistik', [AbsensiApiController::class, 'statistik']);
    Route::get('/status/options', [AbsensiApiController::class, 'getStatusOptions']);
    Route::get('/rekap', [AbsensiApiController::class, 'rekap']);
    Route::get('/kalender', [AbsensiApiController::class, 'kalender']);

    // ⚠️ Route dinamis /{id} paling BAWAH
    // Route::get('/', [AbsensiApiController::class, 'index']);
    // Route::post('/', [AbsensiApiController::class, 'store']);
    // Route::get('/{id}', [AbsensiApiController::class, 'show']);
    // Route::put('/{id}', [AbsensiApiController::class, 'update']);
    // Route::delete('/{id}', [AbsensiApiController::class, 'destroy']);
});

// ✅ Routes Penugasan (BARU)
Route::prefix('penugasan')->group(function () {

    // ⚠️ Route spesifik HARUS di atas route dengan parameter dinamis!

    // Master data
    Route::get('/statistik', [PenugasanApiController::class, 'statistik']);
    Route::get('/master/tarif-by-jenis', [PenugasanApiController::class, 'getTarifByJenis']);
    Route::get('/master/teknisi-list', [PenugasanApiController::class, 'getTeknisiList']);

    // CRUD dasar
    Route::get('/', [PenugasanApiController::class, 'index']);
    Route::get('/{id}', [PenugasanApiController::class, 'show']);

    // Aksi spesifik per penugasan
    Route::post('/{id}/lengkapi-detail', [PenugasanApiController::class, 'lengkapiDetail']);
    Route::put('/{id}/update-detail', [PenugasanApiController::class, 'updateDetail']); // ✅ BARU untuk edit
    Route::post('/{id}/add-item', [PenugasanApiController::class, 'addItem']); // ✅ BARU
    Route::put('/{id}/update-status', [PenugasanApiController::class, 'updateStatus']);
    Route::post('/{id}/upload-foto', [PenugasanApiController::class, 'uploadFoto']);
});

// ✅ Routes Gaji (Mobile)
Route::prefix('gaji')->group(function () {
    Route::get('/riwayat', [GajiApiController::class, 'riwayat']);
    Route::get('/{id}', [GajiApiController::class, 'show']);
});

// ✅ Routes Kasbon (Mobile)
Route::prefix('kasbon')->group(function () {
    Route::get('/riwayat', [KasbonApiController::class, 'riwayat']);
    Route::get('/statistik', [KasbonApiController::class, 'statistik']);
});

// ✅ Routes Dashboard (Mobile)
Route::get('/dashboard', [DashboardApiController::class, 'index']);