<?php

namespace App\Http\Controllers;

use App\Models\JobGrade;
use App\Models\Designation;
use Illuminate\Http\Request;

class JobGradeController extends Controller
{
    public function index()
    {
        $grades = JobGrade::with('designations')->get();

        return view('job_grades.index', compact('grades'));
    }

    public function create()
    {
        $designations = Designation::get();

        return view('job_grades.create', compact('designations'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'range_type' => 'required|in:flat,percentage',
            'notes' => 'nullable|string',
            'min_salary' => 'nullable|numeric',
            'mid_salary' => 'nullable|numeric',
            'max_salary' => 'nullable|numeric',
            'min_percent' => 'nullable|numeric',
            'mid_percent' => 'nullable|numeric',
            'max_percent' => 'nullable|numeric',
        ]);

        // Clean irrelevant fields depending on range_type
        if ($request->range_type === 'flat') {
            $validated['min_percent'] = $validated['mid_percent'] = $validated['max_percent'] = null;
        } else {
            $validated['min_salary'] = $validated['mid_salary'] = $validated['max_salary'] = null;
        }

        $grade = JobGrade::create($validated);

        // Assign selected designations
        if ($request->filled('designation_ids')) {
            Designation::whereIn('id', $request->designation_ids)
                ->update(['job_grade_id' => $grade->id]);
        }

        return redirect()->route('job-grades.index')->with('success', 'Job Grade created successfully.');
    }

    public function edit(JobGrade $jobGrade)
    {
        $designations = Designation::get();

        return view('job_grades.edit', compact('jobGrade', 'designations'));
    }

    public function update(Request $request, JobGrade $jobGrade)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'range_type' => 'required|in:flat,percentage',
            'notes' => 'nullable|string',
            'min_salary' => 'nullable|numeric',
            'mid_salary' => 'nullable|numeric',
            'max_salary' => 'nullable|numeric',
            'min_percent' => 'nullable|numeric',
            'mid_percent' => 'nullable|numeric',
            'max_percent' => 'nullable|numeric',
        ]);

        // Clean irrelevant fields depending on range_type
        if ($request->range_type === 'flat') {
            $validated['min_percent'] = $validated['mid_percent'] = $validated['max_percent'] = null;
        } else {
            $validated['min_salary'] = $validated['mid_salary'] = $validated['max_salary'] = null;
        }

        $jobGrade->update($validated);

        // Reset old designations
        Designation::where('job_grade_id', $jobGrade->id)->update(['job_grade_id' => null]);

        // Assign selected ones
        if ($request->filled('designation_ids')) {
            Designation::whereIn('id', $request->designation_ids)
                ->update(['job_grade_id' => $jobGrade->id]);
        }

        return redirect()->route('job-grades.index')->with('success', 'Job Grade updated successfully.');
    }

    public function destroy(JobGrade $jobGrade)
    {
        $jobGrade->delete();

        return redirect()->route('job-grades.index')->with('success', 'Job Grade deleted successfully.');
    }
}
