<?php

namespace App\Controllers;

use App\Modules\Auth\Services\AuthService;
use CodeIgniter\HTTP\ResponseInterface;

/**
 * Web Dashboard Page Controller
 *
 * Serves /dashboard with TailAdmin layout. Requires authenticated session.
 */
class DashboardPageController extends BaseController
{
    public function index(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/profile
     * Halaman profile user: info akun + form ganti password.
     */
    public function profile(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/attendance/reports
     * Attendance reports index: pick date, list schedules, link to report per schedule.
     */
    public function attendanceReports(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/attendance/report/{scheduleId}
     * Schedule attendance report page (expected, present, late, absent).
     */
    public function attendanceReport($scheduleId): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/devices
     * Devices list page (ADMIN only).
     */
    public function devices(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/schedule/today
     * Daily schedule page (today's schedules, role-filtered).
     */
    public function scheduleToday(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/discipline
     * Poin pelanggaran / disiplin siswa (guru, wali, admin).
     */
    public function discipline(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/parent
     * Portal Orang Tua - untuk melihat data anak (absensi, pelanggaran).
     */
    public function parent(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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

    /**
     * GET /dashboard/presence-settings
     * Pengaturan presensi terpusat: koordinat sekolah + jam masuk & jam pulang (ADMIN only).
     */
    public function presenceSettings(): ResponseInterface
    {
        $authService = new AuthService();
        $user       = $authService->currentUser();

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

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