<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;

class EmployeeController extends Controller
{
    /**
     * Display a listing of employees.
     */
    public function index()
    {
        $employees = User::where('status', 'aktif')
            ->orderBy('name')
            ->get();

        return response()->json([
            'success' => true,
            'data' => $employees
        ]);
    }

    /**
     * Store a newly created employee.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'role' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'employee_id' => 'required|string|max:50|unique:users',
            'address' => 'nullable|string',
            'hire_date' => 'nullable|date',
            'password' => 'required|string|min:8',
        ]);

        $employee = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'role' => $request->role,
            'phone' => $request->phone,
            'employee_id' => $request->employee_id,
            'address' => $request->address,
            'hire_date' => $request->hire_date,
            'password' => Hash::make($request->password),
            'status' => 'aktif',
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Employee created successfully',
            'data' => $employee
        ], 201);
    }

    /**
     * Display the specified employee.
     */
    public function show(User $employee)
    {
        return response()->json([
            'success' => true,
            'data' => $employee
        ]);
    }

    /**
     * Update the specified employee.
     */
    public function update(Request $request, User $employee)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($employee->id)],
            'role' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'employee_id' => ['required', 'string', 'max:50', Rule::unique('users')->ignore($employee->id)],
            'address' => 'nullable|string',
            'hire_date' => 'nullable|date',
            'status' => 'required|in:aktif,tidak aktif',
        ]);

        $employee->update($request->only([
            'name',
            'email',
            'role',
            'phone',
            'employee_id',
            'address',
            'hire_date',
            'status'
        ]));

        return response()->json([
            'success' => true,
            'message' => 'Employee updated successfully',
            'data' => $employee
        ]);
    }

    /**
     * Remove the specified employee.
     */
    public function destroy(User $employee)
    {
        // Soft delete by setting status to tidak aktif
        $employee->update(['status' => 'tidak aktif']);

        return response()->json([
            'success' => true,
            'message' => 'Employee deactivated successfully'
        ]);
    }

    /**
     * Get employee attendance history.
     */
    public function attendanceHistory(User $employee, Request $request)
    {
        $request->validate([
            'start_date' => 'nullable|date',
            'end_date' => 'nullable|date|after_or_equal:start_date',
        ]);

        $query = $employee->attendances()->orderBy('date', 'desc');

        if ($request->start_date) {
            $query->whereDate('date', '>=', $request->start_date);
        }

        if ($request->end_date) {
            $query->whereDate('date', '<=', $request->end_date);
        }

        $attendances = $query->paginate(30);

        return response()->json([
            'success' => true,
            'data' => $attendances
        ]);
    }
}
