<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Professor extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'num_prof',
        'first_name',
        'last_name',
        'email',
        'type',
        'dept_id'
    ];


    public function sections() {
        return $this->belongsToMany(Section::class)->withPivot('percent', 'schedule', 'eval');
    }

    public function semesters() {
        return $this->belongsToMany(Semester::class)->withPivot('admin_load', 'investigative_load');
    }

    public function getFullNameAttribute() {
        return "{$this->first_name} {$this->last_name}";
    }

    // public function getSectionLoad(Section $section) {
        // return ($section->pivot->percent ?? 1.0) * (($section->credits ?? 0.0) + calcExtraCredits($section->credits, $section->student_count));
    // }

    public function getAcademicLoad(Semester $semester) {
        $prof_sections = $this->sections->where('semester_code', '=', $semester->code);
        return $prof_sections->reduce(function ($carry, $section) {
            return $carry + ($section->pivot->percent ?? 1.0) * (($section->credits ?? 0.0) + $section->extra_credits);
        }, 0);
    }

}
