<?php

namespace App\Http\Requests;

use App\Concerns\EmployeeDocumentValidationRules;
use App\Models\Customer;
use App\Support\CashSaleCustomer;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreCustomerRequest extends FormRequest
{
    use EmployeeDocumentValidationRules;

    public function authorize(): bool
    {
        return (bool) $this->user()?->can('customers.create');
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_active' => $this->has('is_active')
                ? $this->boolean('is_active')
                : true,
        ]);
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'full_name' => ['required', 'string', 'max:255'],
            'national_card_number' => [
                'required',
                'string',
                'max:50',
                Rule::unique(Customer::class, 'national_card_number'),
                Rule::notIn([CashSaleCustomer::NATIONAL_CARD_NUMBER]),
            ],
            'address' => ['required', 'string', 'max:1000'],
            'supply_zone' => ['required', 'string', 'max:255'],
            'contact_number' => ['required', 'string', 'max:30'],
            'is_active' => ['required', 'boolean'],
            'image' => $this->employeeImageRules(required: false),
            'national_card' => $this->employeeImageRules(required: false),
            'car_image' => $this->employeeImageRules(required: false),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'national_card_number' => 'national card number',
            'contact_number' => 'contact number',
            'supply_zone' => 'supply zone',
            'national_card' => 'national card image',
            'image' => 'customer image',
            'car_image' => 'car image',
        ];
    }
}
