<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Materi;
use App\Models\Tugas;
use App\Models\Mengajar;
use App\Models\Guru;
use Illuminate\Http\Request;

class MateriTugasController extends Controller
{
    // ===================================================
    // MATERI
    // ===================================================

    /**
     * History semua materi dari semua guru
     */
    public function historyMateri(Request $request)
    {
        $query = Materi::with(['mengajar.mapel', 'mengajar.kelas', 'mengajar.guru']);

        // Filter by guru
        if ($request->filled('id_guru')) {
            $query->whereHas('mengajar', fn($q) => $q->where('id_guru', $request->id_guru));
        }

        // Filter by mapel
        if ($request->filled('id_mapel')) {
            $query->whereHas('mengajar', fn($q) => $q->where('id_mapel', $request->id_mapel));
        }

        // Search judul
        if ($request->filled('search')) {
            $query->where('judul_materi', 'like', '%' . $request->search . '%');
        }

        $materiList = $query->orderBy('created_at', 'desc')->paginate(15)->appends($request->all());
        $gurus      = Guru::orderBy('nama')->get();

        return view('admin.materi.history', compact('materiList', 'gurus'));
    }

    /**
     * Hapus materi (admin)
     */
    public function destroyMateri($id)
    {
        $materi = Materi::findOrFail($id);

        if ($materi->lampiran_materi && \Storage::disk('public')->exists($materi->lampiran_materi)) {
            \Storage::disk('public')->delete($materi->lampiran_materi);
        }

        $materi->delete();

        return redirect()->route('admin.materi.history')
            ->with('success', 'Materi berhasil dihapus.');
    }

    // ===================================================
    // TUGAS
    // ===================================================

    /**
     * History semua tugas dari semua guru
     */
    public function historyTugas(Request $request)
    {
        $query = Tugas::with(['mengajar.mapel', 'mengajar.kelas', 'mengajar.guru', 'pengumpulanTugas']);

        // Filter by guru
        if ($request->filled('id_guru')) {
            $query->whereHas('mengajar', fn($q) => $q->where('id_guru', $request->id_guru));
        }

        // Search judul
        if ($request->filled('search')) {
            $query->where('judul_tugas', 'like', '%' . $request->search . '%');
        }

        $tugasList = $query->orderBy('created_at', 'desc')->paginate(15)->appends($request->all());
        $gurus     = Guru::orderBy('nama')->get();

        return view('admin.tugas.history', compact('tugasList', 'gurus'));
    }

    /**
     * Detail tugas + daftar pengumpulan siswa
     */
    public function detailTugas($id)
    {
        $tugas = Tugas::with([
                'mengajar.mapel',
                'mengajar.kelas',
                'mengajar.guru',
                'pengumpulanTugas.siswa',
            ])
            ->findOrFail($id);

        return view('admin.tugas.detail', compact('tugas'));
    }

    /**
     * Hapus tugas (admin)
     */
    public function destroyTugas($id)
    {
        $tugas = Tugas::findOrFail($id);
        $tugas->delete();

        return redirect()->route('admin.tugas.history')
            ->with('success', 'Tugas berhasil dihapus.');
    }
}