<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SpecialDutyMaster;

class SpecialDutyMasterController extends Controller
{

    /**
     * List with Pagination + Search
     */
    public function index(Request $request)
    {
        try {

            $search = $request->get('search');

            $query = SpecialDutyMaster::query();

            if ($search) {
                $query->where(function ($q) use ($search) {
                    $q->where('SDt_Name', 'like', "%{$search}%")
                      ->orWhere('SDt_Code', 'like', "%{$search}%");
                });
            }

            $data = $query->orderBy('SDt_Id', 'desc')
                          ->paginate(15);

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'Special duty list fetched successfully.'
            ]);

        } catch (\Exception $e) {

            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch data.',
                'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
            ], 500);
        }
    }


    /**
     * All Data without Pagination
     */
    public function all()
    {
        $data = SpecialDutyMaster::orderBy('SDt_Name')->get();

        return response()->json([
            'success' => true,
            'data' => $data
        ]);
    }


    public function store(Request $request)
    {
        try {

            $validated = $request->validate([
                'SDt_Code'      => 'nullable|string|max:20|unique:special_duty_master,SDt_Code',
                'SDt_Name'      => 'required|string|max:50',
                'SDt__Desc'     => 'nullable|string|max:150',
                'SDt__SDate'    => 'nullable|date',
                'SDt__EDate'    => 'nullable|date|after_or_equal:SDt__SDate',
                'SDt_Fees'      => 'required|boolean',
                'Is_Reimburse'  => 'required|boolean',
                'Is_Active'     => 'required|boolean',
            ]);

            $data = SpecialDutyMaster::create($validated);

            return response()->json([
                'success' => true,
                'message' => 'Special duty created successfully.',
                'data'    => $data
            ], 201);
        } catch (\Exception $e) {

            return response()->json([
                'success' => false,
                'message' => 'Failed to create special duty.',
                'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
            ], 500);
        }
    }



    /**
     * Show Single
     */
    public function show($id)
    {
        $data = SpecialDutyMaster::find($id);

        if (!$data) {
            return response()->json([
                'success' => false,
                'message' => 'Record not found.'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $data
        ]);
    }


   public function update(Request $request, $id)
    {
        try {

            $data = SpecialDutyMaster::find($id);

            if (!$data) {
                return response()->json([
                    'success' => false,
                    'message' => 'Record not found.'
                ], 404);
            }

            $validated = $request->validate([
                'SDt_Code'      => 'sometimes|nullable|string|max:20|unique:special_duty_master,SDt_Code,' . $id . ',SDt_Id',
                'SDt_Name'      => 'sometimes|required|string|max:50',
                'SDt__Desc'     => 'sometimes|nullable|string|max:150',
                'SDt__SDate'    => 'sometimes|nullable|date',
                'SDt__EDate'    => 'sometimes|nullable|date|after_or_equal:SDt__SDate',
                'SDt_Fees'      => 'sometimes|required|boolean',
                'Is_Reimburse'  => 'sometimes|required|boolean',
                'Is_Active'     => 'sometimes|required|boolean',
            ]);

            $data->update($validated);

            return response()->json([
                'success' => true,
                'message' => 'Special duty updated successfully.',
                'data'    => $data
            ]);
        } catch (\Exception $e) {

            return response()->json([
                'success' => false,
                'message' => 'Failed to update special duty.',
                'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
            ], 500);
        }
    }



    /**
     * Delete
     */
    public function destroy($id)
    {
        $data = SpecialDutyMaster::find($id);

        if (!$data) {
            return response()->json([
                'success' => false,
                'message' => 'Record not found.'
            ], 404);
        }

        $data->delete();

        return response()->json([
            'success' => true,
            'message' => 'Special duty deleted successfully.'
        ]);
    }

}
