<?php

namespace App\Models;

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

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

    protected $casts = [
        'options' => 'array',
        'is_active' => 'boolean',
    ];

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

    public function maxPoints(): int
    {
        return collect($this->options)->max('points') ?? 0;
    }

    public function pointsFor(?string $optionKey): int
    {
        return (int) (collect($this->options)->firstWhere('key', $optionKey)['points'] ?? 0);
    }
}
