<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreSalaryIncrementRequest;
use App\Models\SalaryIncrement;
use App\Models\User;
use Inertia\Inertia;
use Inertia\Response;

class SalaryIncrementController extends Controller
{
    public function index(): Response
    {
        return Inertia::render('Admin/Payroll/Increments/Index', [
            'increments' => SalaryIncrement::with(['employee', 'approvedBy'])
                ->orderByDesc('created_at')
                ->get(),
            'employees' => User::where('type', 'employee')
                ->where('status', 'active')
                ->whereHas('salaryAssignment')
                ->with('salaryAssignment')
                ->get(),
        ]);
    }

    public function store(StoreSalaryIncrementRequest $request)
    {
        $data = $request->validated();
        $employee = User::findOrFail($data['employee_id']);
        $assignment = $employee->salaryAssignment;

        if (!$assignment) {
            return back()->with('error', 'Employee must have a salary assignment before receiving an increment.');
        }

        SalaryIncrement::create([
            'employee_id' => $employee->id,
            'previous_salary' => $assignment?->gross_salary ?? 0,
            'new_salary' => $data['new_salary'],
            'increment_percentage' => $assignment && $assignment->gross_salary > 0
                ? round((($data['new_salary'] - $assignment->gross_salary) / $assignment->gross_salary) * 100, 4)
                : 0,
            'effective_date' => $data['effective_date'],
            'reason' => $data['reason'] ?? null,
            'status' => 'pending',
        ]);

        return back()->with('success', 'Salary increment submitted for approval.');
    }

    public function update(StoreSalaryIncrementRequest $request, SalaryIncrement $salaryIncrement)
    {
        if ($salaryIncrement->status !== 'pending') {
            return back()->with('error', 'Only pending increments can be edited.');
        }

        $data = $request->validated();
        $employee = User::findOrFail($data['employee_id']);
        $assignment = $employee->salaryAssignment;

        if (!$assignment) {
            return back()->with('error', 'Employee must have a salary assignment before receiving an increment.');
        }

        $salaryIncrement->update([
            'employee_id' => $data['employee_id'],
            'previous_salary' => $assignment?->gross_salary ?? $salaryIncrement->previous_salary,
            'new_salary' => $data['new_salary'],
            'increment_percentage' => $assignment && $assignment->gross_salary > 0
                ? round((($data['new_salary'] - $assignment->gross_salary) / $assignment->gross_salary) * 100, 4)
                : 0,
            'effective_date' => $data['effective_date'],
            'reason' => $data['reason'] ?? null,
        ]);

        return back()->with('success', 'Salary increment updated.');
    }

    public function destroy(SalaryIncrement $salaryIncrement)
    {
        if ($salaryIncrement->status !== 'pending') {
            return back()->with('error', 'Only pending increments can be deleted.');
        }

        $salaryIncrement->delete();

        return back()->with('success', 'Salary increment deleted.');
    }
}
