<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserAttempt extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = [
        'start_time' => 'datetime',
        'end_time' => 'datetime'
    ];

    public function academy()
    {
        return $this->belongsTo(Academy::class);
    }

    //For memory matrix, we punish the users who stuck on lower levels by deduction fraction of their points regarding on which
    //level they are. The final foruma is: (correct_cells - (correct_cells/level) + level) / 2.
    //We divide by 2 in order to balance each game's contribution toward the final result
    public function getMemoryMatrixResultAttribute($attribute)
    {
        if (is_numeric($attribute) && is_numeric($this->memory_matrix_level)) {
            return round(($attribute - ($attribute / $this->memory_matrix_level) + $this->memory_matrix_level) / 2);
        }

        return $attribute;
    }

    //For speed arithmetic, each correct answer is valued 2 points
    public function getSpeedArithmeticResultAttribute($attribute)
    {
        if (is_numeric(($attribute))) {
            return $attribute * 2;
        }

        return $attribute;
    }
}
