<?php

namespace App\Http\Controllers;

use App\Models\EmployeeSalary;
use App\Models\EmployeeSetup;
use Illuminate\Http\Request;
use Inertia\Inertia;

class EmployeeSalaryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $salaries = EmployeeSalary::with('employee.user')->orderBy('created_at', 'desc')->get();
        $employees = EmployeeSetup::orderBy('id')->with('user')->get();

        return Inertia::render('Employee/EmployeeSalary', [
            'salaries' => $salaries,
            'employees' => $employees,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'employee_id' => 'required|exists:employee_setups,id',
            'salary_month' => 'required|date',
            'basic_salary' => 'required|numeric|min:0',
            'allowance' => 'nullable|numeric|min:0',
            'bonus' => 'nullable|numeric|min:0',
            'deduction' => 'nullable|numeric|min:0',
            'net_salary' => 'required|numeric|min:0',
            'payment_date' => 'nullable|date',
            'payment_status' => 'required|in:pending,paid,cancelled',
            'remarks' => 'nullable|string',
        ]);

        $validated['created_by'] = auth()->id();

        EmployeeSalary::create($validated);

        $salaries = EmployeeSalary::with('employee.user')->orderBy('created_at', 'desc')->get();

        return response()->json([
            'success' => 'Salary record created successfully',
            'salaries' => $salaries,
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, EmployeeSalary $employeeSalary)
    {
        $validated = $request->validate([
            'employee_id' => 'required|exists:employee_setups,id',
            'salary_month' => 'required|date',
            'basic_salary' => 'required|numeric|min:0',
            'allowance' => 'nullable|numeric|min:0',
            'bonus' => 'nullable|numeric|min:0',
            'deduction' => 'nullable|numeric|min:0',
            'net_salary' => 'required|numeric|min:0',
            'payment_date' => 'nullable|date',
            'payment_status' => 'required|in:pending,paid,cancelled',
            'remarks' => 'nullable|string',
        ]);

        $validated['updated_by'] = auth()->id();

        $employeeSalary->update($validated);

        $salaries = EmployeeSalary::with('employee.user')->orderBy('created_at', 'desc')->get();

        return response()->json([
            'success' => 'Salary record updated successfully',
            'salaries' => $salaries,
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(EmployeeSalary $employeeSalary)
    {
        $employeeSalary->delete();

        $salaries = EmployeeSalary::with('employee.user')->orderBy('created_at', 'desc')->get();

        return response()->json([
            'success' => 'Salary record deleted successfully',
            'salaries' => $salaries,
        ]);
    }
}
