<?php

namespace App\Http\Requests;

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

class StoreDealerRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('dealers.create');
    }

    public function rules(): array
    {
        $dealerId = $this->route('dealer')?->id;

        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->route('dealer')?->user_id)],
            'password' => [$this->isMethod('post') ? 'required' : 'nullable', 'string', 'min:8'],
            'code' => ['required', 'string', 'max:50', Rule::unique('dealers', 'code')->ignore($dealerId)],
            'company_name' => ['required', 'string', 'max:255'],
            'tax_no' => ['nullable', 'string', 'max:20'],
            'tax_office' => ['nullable', 'string', 'max:255'],
            'phone' => ['required', 'string', 'max:20'],
            'address' => ['nullable', 'string'],
            'il' => ['required', 'string', 'max:100'],
            'ilce' => ['required', 'string', 'max:100'],
            'type' => ['required', Rule::in(['sales', 'content', 'sales_content'])],
            'status' => ['required', Rule::in(['active', 'passive'])],
            'parent_dealer_id' => ['nullable', 'exists:dealers,id'],
            'notes' => ['nullable', 'string'],
        ];
    }
}
