<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Parnet;
use App\Models\Classe;
use App\Models\Bulletin;
use App\Models\Score;
use App\Models\GenMoyenne;

class Eleve extends Model
{
    use HasFactory;

    protected $fillable = ['first_name','last_name','email','phone', 'parent_id','date','code'];

    // One-to-many relationship with parent
    public function parent()
    {
        return $this->belongsTo(Parnet::class);
    }
 public function genmoyenne()
{
    return $this->hasOne(GenMoyenne::class, 'eleve_id', 'id');
}

    // Many-to-many relationship with classes
    public function classes()
    {
        return $this->belongsToMany(Classe::class, 'eleve_class');
    }

    // One-to-many relationship with bulletins
    public function bulletins()
    {
        return $this->hasMany(Bulletin::class);
    }

    // One-to-many relationship with scores
    public function scores()
    {
        return $this->hasMany(Score::class);
    }
    
        public function genSubjects()
    {
        return $this->hasMany(GenSubject::class);
    }
    
        public function getSemestre1Attribute()
    {
        return Score::isDispenserActive($this->id, 1);
    }

    public function getSemestre2Attribute()
    {
        return Score::isDispenserActive($this->id, 2);
    }

    public function getSemestre3Attribute()
    {
        return Score::isDispenserActive($this->id, 3);
    }
}

