<?php

namespace App\Models;

use App\Enums\BadgesStatusEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Badge extends Model
{
    use \Cviebrock\EloquentSluggable\Sluggable, HasFactory;

    protected $fillable = [
        'title',
        'slug',
        'description',
        'icon',
        'criteria',
        'status',
    ];

    protected $casts = [
        'criteria' => 'array',
        'status' => BadgesStatusEnum::class,
    ];

    /**
     * Return the sluggable configuration array for this model.
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title',
            ],
        ];
    }

    public function users()
    {

        return $this->belongsToMany(User::class, 'user_badges', 'badge_id', 'user_id')
            ->withTimestamps()
            ->withPivot('earned_at');
    }
}
