<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * @property int $id
 * @property int $ticket_department_id
 * @property string $name
 * @property string $type
 * @property bool $required
 * @property array<array-key, mixed>|null $options
 * @property int $sort_order
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\TicketDepartment $ticketDepartment
 *
 * @method static \Database\Factories\TicketDepartmentCustomFieldFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField ordered()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereOptions($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereRequired($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereSortOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereTicketDepartmentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketDepartmentCustomField whereUpdatedAt($value)
 *
 * @mixin \Eloquent
 */
class TicketDepartmentCustomField extends Model
{
    use HasFactory;

    protected $fillable = [
        'ticket_department_id',
        'name',
        'type',
        'required',
        'options',
        'sort_order',
    ];

    protected $casts = [
        'required' => 'boolean',
        'options' => 'array',
        'sort_order' => 'integer',
    ];

    /**
     * Get the ticket department that owns the custom field.
     */
    public function ticketDepartment(): BelongsTo
    {
        return $this->belongsTo(TicketDepartment::class);
    }

    /**
     * Scope to order by sort order
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }

    /**
     * Get field type options
     */
    public static function getFieldTypes(): array
    {
        return [
            'text' => 'Metin',
            'textarea' => 'Çoklu Satır Metin',
            'select' => 'Seçim Listesi',
            'checkbox' => 'Onay Kutusu',
            'radio' => 'Radyo Buton',
            'email' => 'E-posta',
            'number' => 'Sayı',
            'date' => 'Tarih',
        ];
    }

    /**
     * Check if field type needs options
     */
    public function needsOptions(): bool
    {
        return in_array($this->type, ['select', 'radio']);
    }
}
