<?php

namespace App\Services\Admin\AcademicClasses;

use App\Models\Schedule;
use Illuminate\Database\Eloquent\Collection;

class ScheduleService
{
    public function all(): Collection
    {
        return Schedule::query()
            ->select(['id', 'course_class_id', 'day_of_week', 'start_time', 'end_time', 'room', 'online_link'])
            ->with('courseClass.course:id,code,name')
            ->orderBy('start_time')
            ->get();
    }

    public function create(array $data): Schedule
    {
        return Schedule::create([
            'course_class_id' => $data['course_class_id'],
            'day_of_week' => $data['day_of_week'] ?? null,
            'start_time' => $data['start_time'] ?? null,
            'end_time' => $data['end_time'] ?? null,
            'room' => $data['room'] ?? null,
            'online_link' => $data['online_link'] ?? null,
        ]);
    }

    public function update(Schedule $schedule, array $data): Schedule
    {
        $schedule->course_class_id = $data['course_class_id'];
        $schedule->day_of_week = $data['day_of_week'] ?? null;
        $schedule->start_time = $data['start_time'] ?? null;
        $schedule->end_time = $data['end_time'] ?? null;
        $schedule->room = $data['room'] ?? null;
        $schedule->online_link = $data['online_link'] ?? null;
        $schedule->update();

        return $schedule;
    }

    public function delete(Schedule $schedule): bool
    {
        return $schedule->delete();
    }
}
