<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\TaMaRateMaster;
use Illuminate\Support\Facades\Validator;


class TaMaRateMasterController extends Controller
{
    // 🔹 List with pagination + search
    public function index(Request $request)
    {
        $query = TaMaRateMaster::with(['empGrade', 'cityGrade']);

        // 🔍 Search
        if ($request->search) {
            $search = $request->search;

            $query->where(function ($q) use ($search) {
                $q->where('TA', 'like', "%$search%")
                  ->orWhere('MA', 'like', "%$search%")
                  ->orWhereHas('empGrade', function ($q2) use ($search) {
                      $q2->where('EGM_Name', 'like', "%$search%");
                  })
                  ->orWhereHas('cityGrade', function ($q3) use ($search) {
                      $q3->where('CGR_Name', 'like', "%$search%");
                  });
            });
        }

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

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

    // 🔹 Store
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'EGM_Id' => 'nullable|exists:emp_grade_master,EGM_Id',
            'CGR_Id' => 'nullable|exists:city_grade,CGR_Id',
            'TA' => 'nullable|numeric',
            'MA' => 'nullable|numeric',
            'TMR_Date' => 'nullable|date'
        ]);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'errors' => $validator->errors()]);
        }

        $data = TaMaRateMaster::create($request->all());

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

    // 🔹 Show
    public function show($id)
    {
        $data = TaMaRateMaster::with(['empGrade', 'cityGrade'])->find($id);

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

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

    // 🔹 Update
    public function update(Request $request, $id)
    {
        $record = TaMaRateMaster::find($id);

        if (!$record) {
            return response()->json(['success' => false, 'message' => 'Not found']);
        }

        $validator = Validator::make($request->all(), [
            'EGM_Id' => 'nullable|exists:emp_grade_master,EGM_Id',
            'CGR_Id' => 'nullable|exists:city_grade,CGR_Id',
            'TA' => 'nullable|numeric',
            'MA' => 'nullable|numeric',
            'TMR_Date' => 'nullable|date'
        ]);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'errors' => $validator->errors()]);
        }

        $record->update($request->all());

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

    // 🔹 Delete (Soft Delete + Restrict handled by FK)
    public function destroy($id)
    {
        $record = TaMaRateMaster::find($id);

        if (!$record) {
            return response()->json(['success' => false, 'message' => 'Not found']);
        }

        $record->delete();

        return response()->json(['success' => true, 'message' => 'Deleted successfully']);
    }
}
