<?php

namespace App\Http\Controllers;

use App\Models\Prestasi;
use App\Models\Santri;
use App\Models\Kelas;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class PrestasiController extends Controller
{
    public function index(Request $request)
    {
        $query = Prestasi::with(['santri', 'kelas'])->latest();

        // kalau ada input search
        if ($request->has('search') && $request->search != '') {
            $query->whereHas('santri', function ($q) use ($request) {
                $q->where('nama', 'like', '%' . $request->search . '%');
                // ganti 'nama' sesuai kolom di tabel santris
            });
        }

        $prestasis = $query->paginate(10)->withQueryString();

        $view = auth()->user()->isGuru() ? 'guru.prestasis.index' : 'prestasis.index';
        return view($view, compact('prestasis'));
    }


    public function create()
    {
        $santris = Santri::all();
        $kelas = Kelas::all();
        $view = auth()->user()->isGuru() ? 'guru.prestasis.create' : 'prestasis.create';
        return view($view, compact('santris', 'kelas'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'santri_id' => 'required|exists:santris,id',
            'kelas_id' => 'required|exists:kelas,id',
            'jenis_prestasi' => 'required|string|max:255',
            'nama_prestasi' => 'required|string|max:255',
            'tingkat' => 'required|string|max:100',
            'peringkat' => 'required|string|max:100',
            'tanggal_prestasi' => 'required|date',
            'deskripsi' => 'nullable|string',
            'sertifikat' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:5120',
        ]);

        $santri = Santri::findOrFail($request->santri_id);

        $sertifikat = null;
        if ($request->hasFile('sertifikat')) {
            $sertifikat = $request->file('sertifikat')->store('sertifikat', 'public');
        }

        Prestasi::create([
            'user_id' => $santri->user_id, // otomatis dari santri
            'santri_id' => $request->santri_id,
            'kelas_id' => $request->kelas_id,
            'jenis_prestasi' => $request->jenis_prestasi,
            'nama_prestasi' => $request->nama_prestasi,
            'tingkat' => $request->tingkat,
            'peringkat' => $request->peringkat,
            'tanggal_prestasi' => $request->tanggal_prestasi,
            'deskripsi' => $request->deskripsi,
            'sertifikat' => $sertifikat,
        ]);

        return redirect()->route('prestasis.index')->with('success', 'Prestasi berhasil ditambahkan.');
    }

    public function edit(Prestasi $prestasi)
    {
        $santris = Santri::all();
        $kelas = Kelas::all();
        return view('prestasis.edit', compact('prestasi', 'santris', 'kelas'));
    }

    public function update(Request $request, Prestasi $prestasi)
    {
        $request->validate([
            'santri_id' => 'required|exists:santris,id',
            'kelas_id' => 'required|exists:kelas,id',
            'jenis_prestasi' => 'required|string|max:255',
            'nama_prestasi' => 'required|string|max:255',
            'tingkat' => 'required|string|max:100',
            'peringkat' => 'required|string|max:100',
            'tanggal_prestasi' => 'required|date',
            'deskripsi' => 'nullable|string',
            'sertifikat' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:5120',
        ]);

        if ($request->hasFile('sertifikat')) {
            if ($prestasi->sertifikat && Storage::disk('public')->exists($prestasi->sertifikat)) {
                Storage::disk('public')->delete($prestasi->sertifikat);
            }

            $prestasi->sertifikat = $request->file('sertifikat')->store('sertifikat', 'public');
        }

        $prestasi->update([
            'santri_id' => $request->santri_id,
            'kelas_id' => $request->kelas_id,
            'jenis_prestasi' => $request->jenis_prestasi,
            'nama_prestasi' => $request->nama_prestasi,
            'tingkat' => $request->tingkat,
            'peringkat' => $request->peringkat,
            'tanggal_prestasi' => $request->tanggal_prestasi,
            'deskripsi' => $request->deskripsi,
            'sertifikat' => $prestasi->sertifikat,
        ]);

        return redirect()->route('prestasis.index')->with('success', 'Prestasi berhasil diperbarui.');
    }

    public function destroy(Prestasi $prestasi)
    {
        if ($prestasi->sertifikat && Storage::disk('public')->exists($prestasi->sertifikat)) {
            Storage::disk('public')->delete($prestasi->sertifikat);
        }

        $prestasi->delete();

        return redirect()->route('prestasis.index')->with('success', 'Prestasi berhasil dihapus.');
    }
}
