<?php

namespace App\Http\Requests\Employee;

use App\Models\Account;
use Illuminate\Foundation\Http\FormRequest;

class UpdateEmployeeRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $employeeId = $this->route('employee')->id;

        $rules = [
            'name' => 'required|string|max:255',
            'email' => 'nullable|email|unique:accounts,email,' . $employeeId,
            'phone' => 'nullable|string|max:20',
            'address' => 'nullable|string',
            'gender' => 'nullable|in:male,female,other',
            'employee_type' => 'required|in:employee,worker,shop_manager,shop_employee',
            'wage_type' => 'required|in:salary,daily_wager,contract',
            'is_active' => 'boolean',
        ];

        // Conditional validation based on employee type
        if (in_array($this->employee_type, ['shop_manager', 'shop_employee'])) {
            $rules['shop_id'] = [
                'required',
                function ($attribute, $value, $fail) {
                    $shopExists = Account::where('id', $value)
                        ->where('model_type', 'shop')
                        ->exists();
                    if (!$shopExists) {
                        $fail('The selected shop is invalid.');
                    }
                },
            ];
        }

        // Conditional validation based on wage type
        if ($this->wage_type === 'salary') {
            $rules['basic_salary'] = 'required|numeric|min:0';
            $rules['working_hours'] = 'required|integer|min:1|max:24';
            $rules['holidays_per_week'] = 'required|integer|min:0|max:7';
        } elseif ($this->wage_type === 'daily_wager') {
            $rules['daily_wage'] = 'required|numeric|min:0';
        } elseif ($this->wage_type === 'contract') {
            $rules['contract_amount'] = 'required|numeric|min:0';
        }

        // Conditional validation for login credentials
        if ($this->allow_login) {
            $rules['email'] = 'required|email|unique:accounts,email,' . $employeeId;
            $rules['username'] = 'required|string|max:255|unique:accounts,username,' . $employeeId;
            $rules['password'] = 'nullable|string|min:8|confirmed';
            $rules['user_role'] = 'required|in:employee,manager,admin,shop_manager';
        }

        return $rules;
    }

    public function messages(): array
    {
        return [
            'name.required' => 'Employee name is required.',
            'employee_type.required' => 'Employee type is required.',
            'wage_type.required' => 'Wage type is required.',
            'shop_id.required' => 'Shop selection is required for this employee type.',
            'basic_salary.required' => 'Basic salary is required for salary employees.',
            'daily_wage.required' => 'Daily wage is required for daily wager employees.',
            'contract_amount.required' => 'Contract amount is required for contract employees.',
            'working_hours.required' => 'Working hours is required for salary employees.',
            'holidays_per_week.required' => 'Holidays per week is required for salary employees.',
            'email.required' => 'Email is required when login is enabled.',
            'username.required' => 'Username is required when login is enabled.',
            'user_role.required' => 'User role is required when login is enabled.',
        ];
    }
}
