<?php

namespace App\Models;

use App\Contracts\Model;
use App\Enums\PirepFieldSource;
use App\Traits\HasSlug;
use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 * @property int                   $id
 * @property string                $name
 * @property string|null           $slug
 * @property string|null           $description
 * @property bool|null             $required
 * @property PirepFieldSource|null $pirep_source
 * @property-read Collection<int, Activity> $activities
 * @property-read int|null $activities_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField wherePirepSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField whereRequired($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PirepField whereSlug($value)
 *
 * @mixin \Eloquent
 */
#[WithoutTimestamps]
class PirepField extends Model
{
    use HasSlug;
    use LogsActivity;

    public $table = 'pirep_fields';

    protected $fillable = [
        'name',
        'slug',
        'description',
        'required',
        'pirep_source',
    ];

    public static array $rules = [
        'name'        => 'required',
        'description' => 'nullable',
    ];

    /**
     * When setting the name attribute, also set the slug
     */
    public function name(): Attribute
    {
        return Attribute::make(
            set: fn ($name): array => [
                'name' => $name,
                'slug' => Str::slug($name),
            ]
        );
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly($this->fillable)
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }

    #[\Override]
    protected function casts(): array
    {
        return [
            'required'     => 'boolean',
            'pirep_source' => PirepFieldSource::class,
        ];
    }
}
