<?php

namespace App\Http\Controllers;

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

class OverTimeRecordController extends Controller
{
    // ✅ List with Pagination + Search + Relations
    public function index(Request $request)
    {
        $perPage = $request->get('per_page', 10);
        $search  = $request->get('search');

        $query = OverTimeRecord::with(['employee', 'hod', 'statusRel', 'rStatusRel']);

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

        $data = $query->paginate($perPage);

        return response()->json($data);
    }

    // ✅ Store
    public function store(Request $request)
    {
        $validated = $request->validate([
            'OTR_Code' => 'nullable|string|max:10',

            'Emp_Id' => 'required|exists:employee_master,Emp_Id',

            'OTR_Date' => 'required|date',
            'OTR_Hrs' => 'required|numeric',

            'OTR_hfDy' => 'nullable|in:FH,SH',

            'Prx_HlDy' => 'nullable|boolean',
            'Sfx_HlDy' => 'nullable|boolean',

            'OTR_HOD' => 'nullable|exists:user_master,Usr_Id',

            'Reason' => 'nullable|string|max:50',

            'Status' => 'nullable|exists:action_master,Act_Id',

            // ✅ NEW FIELDS
            'R_Status' => 'nullable|exists:action_master,Act_Id',
            'Status_Remark' => 'nullable|string',
            'In_Time' => 'nullable|date_format:H:i',
            'Out_Time' => 'nullable|date_format:H:i',
            'Early_Coming' => 'nullable|in:Y,N',
            'Late_Going'   => 'nullable|in:Y,N',
            // 'Work_Extension_Type' => 'nullable|in:EARLY_COMING,LATE_GOING',
        ]);

        // 🔥 If HOD present → set datetime
        if ($request->filled('OTR_HOD')) {
            $validated['OTR_HDT'] = now();
        }

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

        return response()->json([
            'message' => 'Created Successfully',
            'data' => $data->load(['employee', 'hod', 'statusRel', 'rStatusRel'])
        ]);
    }

    // ✅ Show
    public function show($id)
    {
        $data = OverTimeRecord::with(['employee', 'hod', 'statusRel', 'rStatusRel'])
            ->findOrFail($id);

        return response()->json($data);
    }

    // ✅ Update
    public function update(Request $request, $id)
    {
        $data = OverTimeRecord::findOrFail($id);

        $validated = $request->validate([
            'OTR_Code' => 'sometimes|string|max:10',

            'Emp_Id' => 'sometimes|exists:employee_master,Emp_Id',

            'OTR_Date' => 'sometimes|date',
            'OTR_Hrs' => 'sometimes|numeric',

            'OTR_hfDy' => 'nullable|in:FH,SH',

            'Prx_HlDy' => 'nullable|boolean',
            'Sfx_HlDy' => 'nullable|boolean',

            'OTR_HOD' => 'nullable|exists:user_master,Usr_Id',

            'Reason' => 'nullable|string|max:50',

            'Status' => 'nullable|exists:action_master,Act_Id',

            // ✅ NEW FIELDS
            'R_Status' => 'nullable|exists:action_master,Act_Id',
            'Status_Remark' => 'nullable|string',
            'In_Time' => 'nullable|date_format:H:i',
            'Out_Time' => 'nullable|date_format:H:i',
            'Early_Coming' => 'nullable|in:Y,N',
            'Late_Going'   => 'nullable|in:Y,N',
            // 'Work_Extension_Type' => 'sometimes|nullable|in:EARLY_COMING,LATE_GOING',
        ]);

        // 🔥 HOD change → update datetime
        if ($request->has('OTR_HOD') && $request->OTR_HOD != $data->OTR_HOD) {
            $validated['OTR_HDT'] = now();
        }

        $data->update($validated);

        return response()->json([
            'message' => 'Updated Successfully',
            'data' => $data->load(['employee', 'hod', 'statusRel', 'rStatusRel'])
        ]);
    }

    // ✅ Delete
    public function destroy($id)
    {
        $data = OverTimeRecord::findOrFail($id);
        $data->delete();

        return response()->json([
            'message' => 'Deleted Successfully'
        ]);
    }
}
