<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\AbsensiController;
use App\Http\Controllers\Api\PresensiController;
use App\Http\Controllers\Api\CutiController;
use App\Http\Controllers\Api\AnnouncementController;
use App\Http\Controllers\Api\DashboardController;


/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

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

Route::group(['middleware' => 'api', 'prefix' => 'auth'], function($router) {
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::post('/refresh', [AuthController::class, 'refresh']);
    Route::get('/profile', [AuthController::class, 'userProfile']);
});

// Mobile API Routes
Route::prefix('mobile')->group(function () {
    // Auth Routes
    Route::prefix('auth')->group(function () {
        Route::post('login', [AuthController::class, 'login']);
        
        Route::middleware('auth:api')->group(function () {
            Route::post('logout', [AuthController::class, 'logout']);
            Route::post('refresh', [AuthController::class, 'refresh']);
            Route::get('profile', [AuthController::class, 'userProfile']);
            Route::post('update-password', [AuthController::class, 'updatePassword']);
        });
    });

    // Presensi Routes
    Route::middleware('auth:api')->group(function () {
        Route::prefix('presensi')->group(function () {
            Route::get('/', [PresensiController::class, 'getPresensi']);
            Route::post('/save', [PresensiController::class, 'store']);
            Route::post('/izin', [PresensiController::class, 'storeIzin']);
            Route::get('/stats', [PresensiController::class, 'getStats']);
            Route::get('/monthly', [PresensiController::class, 'getMonthlyTotal']);
        });
        
        // Cuti routes
        Route::prefix('cuti')->group(function () {
            Route::get('/', [CutiController::class, 'getAllCuti']);
            Route::post('/', [CutiController::class, 'store']);
            Route::get('/stats', [CutiController::class, 'getStats']);
            Route::get('/pending', [CutiController::class, 'index']);
            Route::post('/{id}/update-status', [CutiController::class, 'updateStatus']);
        });
        
        // Dashboard statistics
        Route::get('dashboard/stats', [PresensiController::class, 'getDashboardStats']);
    });

    // Protected Routes
    // Route::middleware('auth:api')->group(function () {
    //     Route::apiResource('cuti', CutiController::class);
    //     Route::apiResource('absensi', AbsensiController::class);
    // });

    Route::middleware('auth:api')->post('cuti/pending', [CutiController::class, 'store']);
    Route::middleware('auth:api')->get('cuti/pending', [CutiController::class, 'index']);
    Route::middleware('auth:api')->get('cuti', [CutiController::class, 'getAllCuti']);

    Route::group(['middleware' => ['auth:sanctum'], 'prefix' => 'mobile'], function () {
        Route::get('/presensi', [PresensiController::class, 'getPresensi']);
        Route::post('/presensi/save', [PresensiController::class, 'savePresensi']);
    });

    Route::get('/announcements/latest', [AnnouncementController::class, 'latest']);

    Route::get('/dashboard/stats', [DashboardController::class, 'getStats']);

});

Route::group(['prefix' => 'mobile', 'namespace' => 'App\Http\Controllers\Api'], function () {
    // Auth routes
    Route::post('auth/login', 'AuthController@login');
    Route::post('auth/logout', 'AuthController@logout')->middleware('auth:api');
    Route::post('auth/refresh', 'AuthController@refresh')->middleware('auth:api');
    Route::get('auth/profile', 'AuthController@profile')->middleware('auth:api');
    Route::post('auth/update-password', 'AuthController@updatePassword')->middleware('auth:api');

    // Protected routes
    Route::group(['middleware' => 'auth:api'], function () {
        // Dashboard routes
        Route::get('dashboard/user/stats/{userId}', 'DashboardController@getUserStats');
        
        // Cuti routes
        Route::get('cuti/user/{id}', 'CutiController@getUserLeaves');
        Route::post('cuti/store', 'CutiController@store');
        Route::get('cuti/{id}', 'CutiController@show');
        
        // Presensi routes
        Route::get('presensi/user/{id}', 'PresensiController@getUserAttendance');
        Route::post('presensi/save', 'PresensiController@store');
        Route::get('presensi/cek_absen_hari_ini', 'PresensiController@checkTodayAttendance');
    });
});
