<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserBenchmark extends Model
{
    protected $fillable = ["user_id", "training_workout_id", "training_session_id", "level"];

    protected $with = ["user_benchmark_sections"];

    protected $appends = [
        "score"
    ];

    /**
     * ATRIBUTS FICTICIS
     */
    public function getScoreAttribute() {
        $score = 0;
        foreach ($this->user_benchmark_sections as $section) {
            if (TrainingWorkoutSection::find($section->training_workout_section_id)->custom_score){
                $score += ($section->custom_score ? $section->custom_score : 0);
            } else $score += $section->score_section;
        }

        return $score;
    }

    public function user() {
        return $this->belongsTo("App\User", "user_id");
    }

    public function trainingWorkout() {
        return $this->hasOneThrough("App\TrainingWorkout", "App\TrainingSession");
    }

    public function user_benchmark_sections() {
        return $this->hasMany("App\UserBenchmarkSection", "user_benchmark_id", "id");
    }

    public function trainingSession() {
        return $this->belongsTo("App\TrainingSession");
    }

    public function box() {
        return $this->trainingSession->box();
    }
}
