<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\EmployeeLeave;
use App\Models\EmployeeLeaveCredits;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Throwable;

class AttendanceDenyRequestsController extends Controller
{
    public function leave_request(Request $request)
    {
        $leaveUpdate = new LeaveApprovalController();

        $request['status'] = 99;

        $leaveUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
        
        // $data = EmployeeLeave::with(['employee', 'leave_type'])->find($request->id);

        // if($data->status == 99){
        //     return response()->json(['messages' => 'This leave request already denied.'], 404);
        // }

        // if (!$data) {
        //     return response()->json(['messages' => 'No data found.'], 404);
        // }
        
        // $request['status'] = 99;

        // if($request->status == 99){
        //     if($data->leave_id != 1){
        //         $leave_credits = EmployeeLeaveCredits::where([
        //             'employee_id' => $data->employee_id,
        //             'enabled' => 1,
        //             'leave_type_id' => $data->leave_id
        //         ])->first();
                
        //         $remaining_leave = $data->days + $leave_credits->remaining;
    
        //         $updateData = [
        //             'remaining' => $remaining_leave 
        //         ];
                
        //         try {
    
        //             DB::connection()->beginTransaction();
    
        //             $leave_credits->remaining = $remaining_leave;
    
        //             $leave_credits->save();
    
        //             DB::connection()->commit();
    
        //         } catch(Throwable $e) {
        //             DB::rollBack();
        //             return response()->json([
        //                 'status' => false,
        //                 'message' => 'Unable to process request. Please try again.',
        //                 'data' => $e->getMessage()
        //             ], Response::HTTP_INTERNAL_SERVER_ERROR);
        //         }
                
        //     }
        // }

        // DB::beginTransaction();

        // try {
        //     $request->status == 2 ? $request['approved_at'] = date('Y-m-d') : '';

        //     $data->fill($request->all())->save();
            
        //     $data->touch();

        //     $employee_id_for_notification = $request->status == 1 ? $data->approver_id : $data->employee_id;

        //     $send_notification = new LeaveApprovalController();

        //     $send_notification->sendNotification($employee_id_for_notification, $data);

        //     DB::commit();

        //     return response()->json([
        //         'message' => 'Record Successfully updated!',
        //         'status' => 'success',
        //         'data' => $data,
        //     ], 201);

        // } catch (Throwable $e) {
        //     DB::rollback();
        //     return response()->json([
        //         'status'  => false,
        //         'message' => 'Unable to process request. Please try again.',
        //         'data'    => $e->getMessage()
        //     ]);
        // }
    }

    public function overtime_request(Request $request)
    {
        $overtimeUpdate = new OvertimeApprovalController();

        $request['status'] = 99;

        $overtimeUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
    }

    public function undertime_request(Request $request)
    {
        $undertimeUpdate = new UndertimeApprovalController();

        $request['status'] = 99;

        $undertimeUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
    }

    public function travel_request(Request $request)
    {
        $travelUpdate = new TravelApprovalController();

        $request['status'] = 99;

        $travelUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
    }

    public function offset_availment_request(Request $request)
    {
        $offsetAvailmentUpdate = new OffsetAvailmentApprovalController();

        $request['status'] = 99;

        $offsetAvailmentUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
    }

    public function offset_earning_request(Request $request)
    {
        $offsetEarningUpdate = new OffsetEarningApprovalController();

        $request['status'] = 99;

        $offsetEarningUpdate->update($request, $request->id);

        return response()->json([
            'message' => 'Record Successfully updated!',
            'status' => 'success',
        ],201);
    }
}
