<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PenaltyMaster;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class PenaltyMasterController extends Controller
{
    // List + Search + Pagination
    public function index(Request $request)
    {
        $search = $request->search;
        $perPage = $request->per_page ?? 10;

        $data = PenaltyMaster::when($search, function ($q) use ($search) {
            $q->where('Pnlty_Code', 'like', "%$search%")
              ->orWhere('Pnlty_Name', 'like', "%$search%");
        })
        ->orderBy('Pnlty_Id', 'desc')
        ->paginate($perPage);

        return response()->json([
            'status' => true,
            'message' => 'Penalty list fetched successfully',
            'data' => $data
        ]);
    }

    public function all()
{
    $data = PenaltyMaster::orderBy('Pnlty_Id', 'desc')->get();

    return response()->json([
        'status'  => true,
        'message' => 'All penalty records fetched successfully',
        'count'   => $data->count(),
        'data'    => $data
    ]);
}

    // Store
    public function store(Request $request)
    {
     $validator = Validator::make($request->all(), [
    // 'Pnlty_Code'    => 'required|unique:penalty_master,Pnlty_Code',
     'Pnlty_Code' => [
            'nullable',
            \Illuminate\Validation\Rule::unique('penalty_master', 'Pnlty_Code')
                ->whereNull('deleted_at')
        ],
    'Pnlty_Name'    => 'required|string|max:150',

    'Rate'          => 'required|numeric',

    'Calc_Basis'    => 'nullable|in:OnOutstanding,OnPrincipal,Fixed',

    'Grace_Days'    => 'nullable|integer|min:0',

    'Is_Compound'   => 'nullable|boolean',

    'Max_Rate'      => 'nullable|numeric',

    'Is_Active'     => 'nullable|boolean',
]);

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

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

        return response()->json([
            'status' => true,
            'message' => 'Created Successfully',
            'data' => $data
        ]);
    }

    // Show
    public function show($id)
    {
        $data = PenaltyMaster::find($id);

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

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

    // Update
public function update(Request $request, $id)
{
    $data = PenaltyMaster::find($id);

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

    $validator = Validator::make($request->all(), [
        // 'Pnlty_Code'    => 'required|unique:penalty_master,Pnlty_Code,' . $id . ',Pnlty_ID',
         'Pnlty_Code' => [
                'nullable',
                \Illuminate\Validation\Rule::unique('penalty_master', 'Pnlty_Code')
                    ->ignore($id, 'Pnlty_Id')
                    ->whereNull('deleted_at')
            ],
        'Pnlty_Name'    => 'required|string|max:150',

        'Rate'          => 'required|numeric',

        'Calc_Basis'    => 'nullable|in:OnOutstanding,OnPrincipal,Fixed',

        'Grace_Days'    => 'nullable|integer|min:0',

        'Is_Compound'   => 'nullable|boolean',

        'Max_Rate'      => 'nullable|numeric',

        'Is_Active'     => 'nullable|boolean',
    ]);

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

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

    return response()->json([
        'status' => true,
        'message' => 'Updated Successfully',
        'data' => $data
    ]);
}

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

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

        $data->delete();

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