<?php

namespace Modules\MedPortal\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * A patient's answers to a questionnaire (SIFA Phase 8). Staff "send" one by
 * creating an empty row; QuestionnaireService fills score + severity on
 * completion.
 */
class MportalQuestionnaireResponse extends Model
{
    protected $table = 'mportal_questionnaire_responses';

    protected $guarded = ['id'];

    protected $casts = [
        'answers'      => 'array',
        'score'        => 'integer',
        'completed_at' => 'datetime',
    ];

    public function questionnaire()
    {
        return $this->belongsTo(MportalQuestionnaire::class, 'questionnaire_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeCompleted($query)
    {
        return $query->whereNotNull('completed_at');
    }
}
