<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $guarded = [
        'id'
    ];

    protected $fillable = [
        'code',
        'title',
        'syllabus',
        'dept_id',
    ];

    public $timestamps = false;

    public function dept() {
        return $this->belongsTo(Dept::class);
    }

    public function semesters() {
        // dd($this->hasManyThrough(Semester::class, Section::class, 'course_id', 'code'));
        // dd($this->hasManyThrough(Semester::class, Section::class, 'course_id', 'code')->get());
        return $this->belongsToMany(Semester::class, 'sections');
    }

    public function sections() {
        return $this->hasMany(Section::class);
    }

    /**
     * Return count of sections in a given semester.
     *
     * @param string $sem_code
     * @return int
     */
    public function getSemesterSectionCount($sem_code) {
        return $this->sections->where('semester_code', '=', $sem_code)->count();
    }
}
