<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class AcademyCourseSection extends Model
{
    protected $fillable = [
        'course_id',
        'title',
        'slug',
        'description',
        'order_num',
        'is_visible',
    ];

    protected $casts = [
        'order_num' => 'integer',
        'is_visible' => 'boolean',
    ];

    public function course(): BelongsTo
    {
        return $this->belongsTo(AcademyCourse::class, 'course_id');
    }

    public function courseLessons(): HasMany
    {
        return $this->hasMany(AcademyCourseLesson::class, 'section_id')
            ->orderBy('order_num')
            ->orderBy('id');
    }

    public function lessons(): BelongsToMany
    {
        return $this->belongsToMany(AcademyLesson::class, 'academy_course_lessons', 'section_id', 'lesson_id')
            ->using(AcademyCourseLesson::class)
            ->withPivot(['course_id', 'order_num', 'is_required', 'access_override', 'unlock_after_lesson_id'])
            ->withTimestamps()
            ->orderBy('academy_course_lessons.order_num')
            ->orderBy('academy_course_lessons.id');
    }
}