<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Macro extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'name',
        'color',
        'trigger_on_startup',
        'image_type',
        'action_count',
        'hidden_at',
        'last_imported_at',
        'last_imported_filename',
    ];

    protected function casts(): array
    {
        return [
            'trigger_on_startup' => 'boolean',
            'hidden_at' => 'datetime',
            'last_imported_at' => 'datetime',
        ];
    }

    public function collections(): BelongsToMany
    {
        return $this->belongsToMany(MacroCollection::class, 'macro_collection_macros')
            ->withPivot('order')
            ->orderBy('macro_collection_macros.order');
    }

    public function assignments(): HasMany
    {
        return $this->hasMany(MacroAssignment::class);
    }

    public function isHidden(): bool
    {
        return $this->hidden_at !== null;
    }
}
