<?php

namespace App\Models;

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

class NilaiUjian extends Model
{
    use HasFactory;

    protected $table = 'nilai_ujian';

    protected $fillable = [
        'ujian_id',
        'siswa_id',
        'nilai_pg',
        'nilai_bs',
        'nilai_essay',
        'nilai_murni',
        'nilai_katrol',
        'nilai_final',
        'is_uploaded',
        'is_essay_filled',
        'is_katrol',
    ];

    protected function casts(): array
    {
        return [
            'nilai_pg'       => 'float',
            'nilai_bs'       => 'float',
            'nilai_essay'    => 'float',
            'nilai_murni'    => 'float',
            'nilai_katrol'   => 'float',
            'nilai_final'    => 'float',
            'is_uploaded'    => 'boolean',
            'is_essay_filled'=> 'boolean',
            'is_katrol'      => 'boolean',
        ];
    }

    public function ujian()
    {
        return $this->belongsTo(Ujian::class);
    }

    public function siswa()
    {
        return $this->belongsTo(Siswa::class);
    }

    public function hitungNilaiMurni(): float
    {
        $pg    = $this->nilai_pg    ?? 0;
        $bs    = $this->nilai_bs    ?? 0;
        $essay = $this->nilai_essay ?? 0;
        return round($pg + $bs + $essay, 2);
    }
}
