<?php

namespace App\Controllers;

use App\Modules\Auth\Services\AuthService;
use App\Modules\Academic\Models\ClassModel;
use CodeIgniter\HTTP\ResponseInterface;

/**
 * Dashboard Academic (admin-only) pages.
 */
class DashboardAcademicController extends BaseController
{
    /**
     * GET /dashboard/academic/settings
     * Academic settings hub (ADMIN only).
     */
    public function academicSettings(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/academic_settings')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/classes
     * Class management page (ADMIN only).
     */
    public function classes(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/classes')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/subjects
     */
    public function subjects(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/subjects')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/teachers
     */
    public function teachers(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/teachers')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/lesson-slots
     */
    public function lessonSlots(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/lesson_slots')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/students
     */
    public function students(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/students')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/dapodik
     */
    public function dapodik(): ResponseInterface
    {
        $authService = new AuthService();
        $user        = $authService->currentUser();
        $data        = ['user' => $user, 'content' => view('dashboard/dapodik')];
        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/schedule-builder
     * Class selector page; then redirect to schedule-builder/{classId}.
     */
    public function scheduleBuilderIndex(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

        $data = [
            'user'    => $user,
            'content' => view('dashboard/schedule_builder_index'),
        ];

        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/schedule-builder/{classId}
     */
    public function scheduleBuilder($classId): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

        $classId = (int) $classId;
        $classModel = new ClassModel();
        $classEntity = $classModel->find($classId);
        $className   = $classEntity ? (string) $classEntity->name : 'Kelas ' . $classId;

        $data = [
            'user'       => $user,
            'classId'    => $classId,
            'className'  => $className,
            'content'    => view('dashboard/schedule_builder', [
                'classId'   => $classId,
                'className' => $className,
            ]),
        ];

        return $this->response->setBody(view('layouts/main', $data));
    }

    /**
     * GET /dashboard/academic/discipline-settings
     * Master poin pelanggaran (ADMIN only, via route filter).
     */
    public function disciplineSettings(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

        $data = [
            'user'    => $user,
            'content' => view('dashboard/discipline_settings'),
        ];

        return $this->response->setBody(view('layouts/main', $data));
    }
}
