<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class AssessmentAttempt extends Model
{
    protected $guarded = [];

    protected $casts = [
        'answers' => 'array',
        'dimension_percents' => 'array',
        'started_at' => 'datetime',
        'completed_at' => 'datetime',
        'percent' => 'float',
    ];

    protected static function booted(): void
    {
        static::creating(function (self $attempt) {
            $attempt->uuid ??= (string) Str::uuid();
            $attempt->started_at ??= now();
        });
    }

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

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

    public function band(): BelongsTo
    {
        return $this->belongsTo(AssessmentBand::class, 'band_id');
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }
}
