<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class AcademyBadge extends Model
{
    protected $fillable = [
        'name',
        'slug',
        'description',
        'icon',
        'badge_type',
        'rules',
        'active',
    ];

    protected $casts = [
        'rules' => 'array',
        'active' => 'boolean',
    ];

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('active', true);
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'academy_user_badges', 'badge_id', 'user_id')
            ->withPivot('awarded_at')
            ->withTimestamps();
    }
}