<?php

namespace Functional\Routines\Models;

use Functional\Routines\Database\Factories\RoutineFactory;
use Functional\Users\Models\User;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Une routine = liste ordonnée d'exercices avec séries/reps/repos, possédée par un utilisateur.
 *
 * @property string $name
 * @property string $slug
 * @property string|null $description
 * @property bool $is_public
 * @property int|null $estimated_minutes
 */
#[UseFactory(RoutineFactory::class)]
class Routine extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id', 'name', 'slug', 'description', 'is_public', 'estimated_minutes',
    ];

    protected $casts = [
        'is_public' => 'boolean',
        'estimated_minutes' => 'integer',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function routineExercises(): HasMany
    {
        return $this->hasMany(RoutineExercise::class)->orderBy('position');
    }
}
