<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class EmotionTag extends Model
{
    protected $fillable = [
        'name',
        'description',
        'is_active',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    /**
     * @return array<int,array<string,mixed>>
     */
    public static function defaults(): array
    {
        return collect(['Calm', 'Fear', 'Greed', 'Revenge', 'Confident', 'Impatient', 'Frustrated', 'Disciplined'])
            ->values()
            ->map(fn (string $name, int $index): array => [
                'name' => $name,
                'description' => null,
                'sort_order' => $index + 1,
            ])
            ->all();
    }

    public static function seedDefaults(): void
    {
        foreach (self::defaults() as $default) {
            self::query()->firstOrCreate(['name' => $default['name']], $default + ['is_active' => true]);
        }
    }

    /**
     * @return Collection<int,self>
     */
    public static function activeOrdered(): Collection
    {
        self::seedDefaults();

        return self::query()
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get();
    }
}
