<?php

namespace App\Models;

use App\Enums\ExerciseDifficulty;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Exercise extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'name',
        'name_en',
        'main_muscle_id',
        'difficulty',
        'description',
        'gif_path',
        'image_path',
        'is_active',
        'created_by',
        'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'difficulty' => ExerciseDifficulty::class,
            'is_active' => 'boolean',
        ];
    }

    public function mainMuscle()
    {
        return $this->belongsTo(Muscle::class, 'main_muscle_id');
    }

    /**
     * عضلات درگیر (علاوه بر عضله اصلی).
     */
    public function involvedMuscles()
    {
        return $this->belongsToMany(Muscle::class, 'exercise_muscles');
    }

    public function equipment()
    {
        return $this->belongsToMany(Equipment::class, 'exercise_equipment');
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function editor()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

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