<?php

namespace App\Http\Controllers;

use App\Models\JobGrade;
use App\Models\MeritIncreaseMatrix;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MeritIncreaseMatrixController extends Controller
{
    public function index()
    {
        $matrices = MeritIncreaseMatrix::with('jobGrade')->get();
        return view('merit_increase_matrices.index', compact('matrices'));
    }

    public function create()
    {
        $jobGrades = JobGrade::pluck('name', 'id');
        return view('merit_increase_matrices.create', compact('jobGrades'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'job_grade_id' => 'required|exists:job_grades,id',
            'performance_tercile' => 'required|string|max:10',
            'pos_min' => 'required|numeric|min:0',
            'pos_max' => 'required|numeric|min:0',
        ]);

        $data = $request->only([
            'job_grade_id',
            'performance_tercile',
            'pos_min',
            'pos_max',

            'outstanding_min',
            'outstanding_target',
            'outstanding_max',
            'exceeded_min',
            'exceeded_target',
            'exceeded_max',
            'achieved_min',
            'achieved_target',
            'achieved_max',
            'partially_min',
            'partially_target',
            'partially_max',
            'below_min',
            'below_target',
            'below_max',
        ]);


        MeritIncreaseMatrix::create($data);

        return redirect()->route('merit-increase-matrices.index')
            ->with('success', __('Merit Increase Matrix created successfully.'));
    }

    public function edit(MeritIncreaseMatrix $meritIncreaseMatrix)
    {
        $jobGrades = JobGrade::pluck('name', 'id');
        return view('merit_increase_matrices.edit', compact('meritIncreaseMatrix', 'jobGrades'));
    }

    public function update(Request $request, MeritIncreaseMatrix $meritIncreaseMatrix)
    {
        $validated = $request->validate([
            'job_grade_id' => 'required|exists:job_grades,id',
            'performance_tercile' => 'required|string|max:10',
            'pos_min' => 'required|numeric|min:0',
            'pos_max' => 'required|numeric|min:0',
        ]);

        $data = $request->only([
            'job_grade_id',
            'performance_tercile',
            'pos_min',
            'pos_max',

            'outstanding_min',
            'outstanding_target',
            'outstanding_max',
            'exceeded_min',
            'exceeded_target',
            'exceeded_max',
            'achieved_min',
            'achieved_target',
            'achieved_max',
            'partially_min',
            'partially_target',
            'partially_max',
            'below_min',
            'below_target',
            'below_max',
        ]);

        $meritIncreaseMatrix->update($data);

        return redirect()->route('merit-increase-matrices.index')
            ->with('success', __('Merit Increase Matrix updated successfully.'));
    }

    public function destroy(MeritIncreaseMatrix $meritIncreaseMatrix)
    {
        $meritIncreaseMatrix->delete();

        return redirect()->route('merit-increase-matrices.index')
            ->with('success', __('Merit Increase Matrix deleted successfully.'));
    }
}
