<?php

namespace App\Http\Requests;

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

class StoreLineRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('lines.create') || $this->user()?->can('lines.edit') || $this->user()?->hasRole('Administrator');
    }

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

        return [
            'phone_number' => ['required', 'string', 'max:30', Rule::unique('lines', 'phone_number')->ignore($id)],
            'iccid' => ['nullable', 'string', 'max:50', Rule::unique('lines', 'iccid')->ignore($id)],
            'sim_serial' => ['nullable', 'string', 'max:50', Rule::unique('lines', 'sim_serial')->ignore($id)],
            'telecom_company_id' => ['required', 'exists:telecom_companies,id'],
            'sim_type' => ['required', Rule::in(['physical', 'esim'])],
            'sale_price' => ['required', 'numeric', 'min:0'],
            'price_reason' => ['nullable', 'string', 'max:255'],
            'notes' => ['nullable', 'string'],
            'status' => ['nullable', Rule::in(['available', 'reserved', 'sold', 'active', 'suspended', 'cancelled', 'returned'])],
        ];
    }
}
