<?php

namespace App\Domains\Catalog\Models;

use App\Domains\Event\Models\EventDate;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Fillable([
    'tenant_codigo',
    'codigo',
    'nombre',
    'is_required',
    'metadata_schema',
    'type',
])]
class Attribute extends Model
{
    use HasFactory;

    protected $table = 'attribute';

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_required' => 'boolean',
            'metadata_schema' => 'array',
            'type' => FieldType::class,
        ];
    }

    /**
     * @return BelongsTo<Tenant, $this>
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
    }

    /**
     * @return HasMany<AttributeOption, $this>
     */
    public function options(): HasMany
    {
        return $this->hasMany(AttributeOption::class, 'attribute_id')->orderBy('sort_order');
    }

    /**
     * @return HasMany<EventDate, $this>
     */
    public function eventDates(): HasMany
    {
        return $this->hasMany(EventDate::class, 'tenant_code', 'tenant_codigo')
            ->orderBy('date')
            ->orderBy('time_start');
    }
}
