<?php

namespace Functional\Tracking\Models;

use Functional\Exercises\Models\Exercise;
use Functional\Tracking\Database\Factories\PersonalRecordFactory;
use Functional\Tracking\Enums\PersonalRecordType;
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;

/**
 * Record personnel d'un utilisateur pour un exercice et un type de mesure donnés.
 *
 * @property PersonalRecordType $type
 */
#[UseFactory(PersonalRecordFactory::class)]
class PersonalRecord extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id', 'exercise_id', 'type', 'value', 'unit', 'achieved_at', 'workout_session_id',
    ];

    protected $casts = [
        'type' => PersonalRecordType::class,
        'value' => 'decimal:2',
        'achieved_at' => 'datetime',
    ];

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

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

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