<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AnnouncementController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\TeacherController;
use App\Http\Controllers\ScheduleController;
use App\Http\Controllers\ClassroomController;
use App\Http\Controllers\TimeslotController;
use App\Http\Controllers\JadwalPublikController;
use App\Http\Controllers\SchoolClassController;
use App\Http\Controllers\SubjectController;
use App\Http\Controllers\Guru\GuruDashboardController;
use App\Http\Controllers\Guru\AbsenSiswaController as GuruAbsenSiswaController;
use App\Http\Controllers\Guru\AbsenMandiriController;
use App\Http\Controllers\Admin\AbsenSiswaController as AdminAbsenSiswaController;
use App\Http\Controllers\Admin\AbsenGuruController;

// ─── Auth ──────────────────────────────────────────────
Route::get('/login', [AuthController::class, 'index'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Redirect root ke login
Route::get('/', fn() => redirect()->route('login'));

// Jadwal publik
Route::get('/jadwal', [JadwalPublikController::class, 'index'])->name('jadwal.publik');

// ─── Admin ─────────────────────────────────────────────
Route::middleware(['auth', 'role:admin'])->prefix('admin')->name('admin.')->group(function () {

    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Data Siswa
    Route::get('/students',           [StudentController::class, 'index'])->name('students.index');
    Route::post('/students',          [StudentController::class, 'store'])->name('students.store');
    Route::get('/students/{id}/edit', [StudentController::class, 'edit'])->name('students.edit');
    Route::put('/students/{id}',      [StudentController::class, 'update'])->name('students.update');
    Route::delete('/students/{id}',   [StudentController::class, 'destroy'])->name('students.destroy');

    // Data Guru
    Route::get('/teachers',           [TeacherController::class, 'index'])->name('teachers.index');
    Route::post('/teachers',          [TeacherController::class, 'store'])->name('teachers.store');
    Route::get('/teachers/{id}/edit', [TeacherController::class, 'edit'])->name('teachers.edit');
    Route::put('/teachers/{id}',      [TeacherController::class, 'update'])->name('teachers.update');
    Route::delete('/teachers/{id}',   [TeacherController::class, 'destroy'])->name('teachers.destroy');

    // Mata Pelajaran
    Route::get('/subjects',         [SubjectController::class, 'index'])->name('subjects.index');
    Route::post('/subjects',        [SubjectController::class, 'store'])->name('subjects.store');
    Route::put('/subjects/{id}',    [SubjectController::class, 'update'])->name('subjects.update');
    Route::delete('/subjects/{id}', [SubjectController::class, 'destroy'])->name('subjects.destroy');

    // Ruang Kelas
    Route::get('/classrooms',         [ClassroomController::class, 'index'])->name('classrooms.index');
    Route::post('/classrooms',        [ClassroomController::class, 'store'])->name('classrooms.store');
    Route::delete('/classrooms/{id}', [ClassroomController::class, 'destroy'])->name('classrooms.destroy');

    // Data Kelas
    Route::get('/classes',           [SchoolClassController::class, 'index'])->name('classes.index');
    Route::post('/classes',          [SchoolClassController::class, 'store'])->name('classes.store');
    Route::get('/classes/{id}/edit', [SchoolClassController::class, 'edit'])->name('classes.edit');
    Route::put('/classes/{id}',      [SchoolClassController::class, 'update'])->name('classes.update');
    Route::delete('/classes/{id}',   [SchoolClassController::class, 'destroy'])->name('classes.destroy');

    // Jam Belajar
    Route::get('/timeslots',         [TimeslotController::class, 'index'])->name('timeslots.index');
    Route::post('/timeslots',        [TimeslotController::class, 'store'])->name('timeslots.store');
    Route::delete('/timeslots/{id}', [TimeslotController::class, 'destroy'])->name('timeslots.destroy');

    // Jadwal Pelajaran
    Route::get('/schedules',                        [ScheduleController::class, 'index'])->name('schedules.index');
    Route::post('/schedules',                       [ScheduleController::class, 'store'])->name('schedules.store');
    Route::get('/schedules/{id}/edit',              [ScheduleController::class, 'edit'])->name('schedules.edit');
    Route::put('/schedules/{id}',                   [ScheduleController::class, 'update'])->name('schedules.update');
    Route::delete('/schedules/{id}',                [ScheduleController::class, 'destroy'])->name('schedules.destroy');
    Route::post('/schedules/{id}/toggle-publish',   [ScheduleController::class, 'togglePublish'])->name('schedules.toggle-publish');
    Route::post('/schedules/publish-all',           [ScheduleController::class, 'publishAll'])->name('schedules.publish-all');

    // Absensi Siswa
    Route::get('/absen/siswa',  [AdminAbsenSiswaController::class, 'index'])->name('absen.siswa');

    // Absensi Guru
    Route::get('/absen/guru',   [AbsenGuruController::class, 'index'])->name('absen.guru');
    Route::post('/absen/guru',  [AbsenGuruController::class, 'store'])->name('absen.guru.store');

    // Pengumuman (dashboard)
    Route::post('/announcements',            [AnnouncementController::class, 'store'])->name('announcements.store');
    Route::post('/announcements/{id}/pin',   [AnnouncementController::class, 'togglePin'])->name('announcements.toggle-pin');
    Route::delete('/announcements/{id}',     [AnnouncementController::class, 'destroy'])->name('announcements.destroy');
});

// ─── Guru ──────────────────────────────────────────────
Route::middleware(['auth', 'role:guru'])->prefix('guru')->name('guru.')->group(function () {

    Route::get('/dashboard', [GuruDashboardController::class, 'index'])->name('dashboard');

    // Absen mandiri guru (selfie + GPS)
    Route::get('/absen/mandiri',  [AbsenMandiriController::class, 'index'])->name('absen.mandiri');
    Route::post('/absen/mandiri', [AbsenMandiriController::class, 'store'])->name('absen.mandiri.store');

    // Presensi kelas binaan
    Route::get('/absen/siswa',  [GuruAbsenSiswaController::class, 'index'])->name('absen.siswa');
    Route::post('/absen/siswa', [GuruAbsenSiswaController::class, 'store'])->name('absen.siswa.store');
});

// ─── Kepala Sekolah ─────────────────────────────────────
Route::middleware(['auth', 'role:kepsek'])->prefix('kepsek')->name('kepsek.')->group(function () {
    Route::get('/dashboard', fn() => view('kepsek.dashboard'))->name('dashboard');
});

// Auth routes (Breeze)
require __DIR__.'/auth.php';
