<?php

namespace App\Http\Requests\Contact;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

	public function rules(): array
	{
		$contactId = $this->route('contact');

		$rules = [
			'name' => ['required', 'string', 'max:255'],
			'contact_person' => ['nullable', 'string', 'max:255'],
			'phone' => ['nullable', 'string', 'max:255'],
			'email' => ['nullable', 'email', 'max:255', Rule::unique('contacts', 'email')->ignore($contactId)],
			'cnic' => ['nullable', 'string', 'max:15', Rule::unique('contacts', 'cnic')->ignore($contactId)],
			'gender' => ['nullable', 'in:male,female,other'],
			'address' => ['nullable', 'string'],
			'rating' => ['nullable', 'numeric', 'min:0', 'max:999.99'],
			'term_type' => ['nullable', 'in:net,advance,partial,fixed,due_on_receipt'],
			'term_value' => ['nullable', 'numeric', 'min:0'],
			'value_unit' => ['nullable', 'in:days,percent,amount'],
			'term_description' => ['nullable', 'string'],
			'is_active' => ['sometimes', 'boolean'],
			'type' => ['required', 'in:employee,vendor,customer,shop_manager,shop_employee'],
			'pay_amount' => ['nullable','numeric','min:0'],
			'incentive_amount' => ['nullable','numeric','min:0'],
			'holidays_per_week' => ['nullable','integer','min:0','max:7'],
			'is_daily_wager' => ['sometimes','boolean'],
			'per_day_amount' => ['nullable','numeric','min:0'],
			'per_hour_amount' => ['nullable','numeric','min:0'],
		];

		// Additional validation for shop managers
		if ($this->input('type') === 'shop_manager') {
			$rules['shop_name'] = ['required', 'string', 'max:255'];
			$rules['city'] = ['required', 'string', 'max:255'];
			$rules['phone'] = ['required', 'string', 'max:255'];
			$rules['email'] = [
				'required',
				'email',
				'max:255',
				Rule::unique('contacts', 'email')->ignore($contactId),
				Rule::unique('users', 'email')->ignore($contactId, 'id')
			];
			$rules['shop_phone'] = ['nullable', 'string', 'max:255'];
			$rules['shop_email'] = ['nullable', 'email', 'max:255'];
			$rules['shop_address'] = ['nullable', 'string'];
			$rules['pay_amount'] = ['required','numeric','min:0'];
			$rules['holidays_per_week'] = ['required','integer','min:0','max:7'];
		}

		// Additional validation for shop employees
		if ($this->input('type') === 'shop_employee') {
			$rules['shop_id'] = ['required', 'exists:shops,id'];
			$rules['phone'] = ['required', 'string', 'max:255'];
			$rules['email'] = [
				'required',
				'email',
				'max:255',
				Rule::unique('contacts', 'email')->ignore($contactId),
				Rule::unique('users', 'email')->ignore($contactId, 'id')
			];
			$rules['pay_amount'] = ['required','numeric','min:0'];
			$rules['holidays_per_week'] = ['required','integer','min:0','max:7'];
		}

		// Email validation for employees
		if ($this->input('type') === 'employee') {
			$rules['email'] = [
				'required',
				'email',
				'max:255',
				Rule::unique('contacts', 'email')->ignore($contactId),
				Rule::unique('users', 'email')->ignore($contactId, 'id')
			];
			$rules['pay_amount'] = ['required','numeric','min:0'];
			$rules['holidays_per_week'] = ['required','integer','min:0','max:7'];
		}

		// Daily wager specifics
		if ($this->boolean('is_daily_wager')) {
			$rules['per_day_amount'] = ['required','numeric','min:0'];
			$rules['per_hour_amount'] = ['required','numeric','min:0'];
		}

		// User creation validation
		if ($this->input('create_user')) {
			$rules['username'] = ['required', 'string', 'min:3', 'max:50', 'unique:users,username'];
			$rules['password'] = ['required', 'string', 'min:8', 'confirmed'];
			$rules['password_confirmation'] = ['required', 'string'];
			$rules['role_id'] = ['required', 'exists:roles,id'];
		}

		return $rules;
	}

	public function messages(): array
	{
		return [
			'type.in' => 'Contact type must be one of: employee, vendor, customer, or shop manager.',
			'shop_name.required' => 'Shop name is required for shop managers.',
			'city.required' => 'City is required for shop managers.',
			'phone.required' => 'Phone is required for shop managers.',
			'email.required' => 'Email is required for employees and shop managers.',
			'email.unique' => 'This email is already registered in the system.',
		];
	}
}
