<?php

namespace App\Models;

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

class Satisfaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'q1',
        'q2',
        'q3',
        'q4',
        'q5',
        'q6',
        'q7',
        'q8',
        'q9',
        'q10',
        'q11',
        'q12',
        'q13',
        'q14',
        'q15',
        'kritik_saran'
    ];

    /**
     * Get the user that owns the satisfaction survey.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the average satisfaction score.
     */
    public function getAverageScoreAttribute()
    {
        $fields = ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10', 'q11', 'q12', 'q13', 'q14', 'q15'];
        $sum = 0;
        foreach ($fields as $field) {
            $sum += $this->$field;
        }
        return $sum / count($fields);
    }
}
