<?php

namespace App\Http\Controllers;

use App\Models\Employee;
use App\Models\EmployeeSalaryHistory;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    public function index()
    {
        $employees = Employee::latest()->paginate(15);
        return view('employees.index', compact('employees'));
    }

    public function create()
    {
        return view('employees.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'          => ['required', 'string', 'max:255'],
            'designation'   => ['nullable', 'string', 'max:255'],
            'phone'         => ['nullable', 'string', 'max:50'],
            'email'         => ['nullable', 'email', 'max:255'],
            'salary_type'   => ['required', 'in:daily,weekly,monthly'],
            'salary_amount' => ['required', 'numeric', 'min:0'],
            'advance_amount'=> ['nullable', 'numeric', 'min:0'],
            'joining_date'  => ['nullable', 'date'],
            'resign_date'   => ['nullable', 'date'],
            'status'        => ['required', 'in:active,inactive'],
            'notes'         => ['nullable', 'string'],
        ]);

        Employee::create($data);

        return redirect()->route('employees.index')->with('success', __('Employee added successfully.'));
    }

    public function edit(Employee $employee)
    {
        $salaryHistories = $employee->salaryHistories()->get();
        return view('employees.edit', compact('employee', 'salaryHistories'));
    }

    public function update(Request $request, Employee $employee)
    {
        $data = $request->validate([
            'name'          => ['required', 'string', 'max:255'],
            'designation'   => ['nullable', 'string', 'max:255'],
            'phone'         => ['nullable', 'string', 'max:50'],
            'email'         => ['nullable', 'email', 'max:255'],
            'salary_type'   => ['required', 'in:daily,weekly,monthly'],
            'salary_amount' => ['required', 'numeric', 'min:0'],
            'advance_amount'=> ['nullable', 'numeric', 'min:0'],
            'joining_date'  => ['nullable', 'date'],
            'resign_date'   => ['nullable', 'date'],
            'status'        => ['required', 'in:active,inactive'],
            'notes'         => ['nullable', 'string'],
        ]);

        // Track salary change
        if ((float)$employee->salary_amount !== (float)$data['salary_amount'] || $employee->salary_type !== $data['salary_type']) {
            EmployeeSalaryHistory::create([
                'employee_id'    => $employee->id,
                'old_salary'     => $employee->salary_amount,
                'new_salary'     => $data['salary_amount'],
                'salary_type'    => $data['salary_type'],
                'effective_date' => now()->toDateString(),
                'notes'          => 'Salary updated',
            ]);
        }

        $employee->update($data);

        return redirect()->route('employees.index')->with('success', 'Employee updated.');
    }

    public function destroy(Employee $employee)
    {
        $employee->delete();
        return redirect()->route('employees.index')->with('success', __('Employee removed successfully.'));
    }
}

