<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Siswa;
use App\Models\Kelas;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class SiswaController extends Controller
{
    public function index(Request $request)
    {
        $query = Siswa::with('kelas');

        // SEARCH
        if ($request->has('search')) {
            $search = $request->search;
            $query->where('nama', 'like', "%$search%")
                  ->orWhere('nisn', 'like', "%$search%");
        }

        // FILTER BY KELAS
        if ($request->has('filter_kelas') && $request->filter_kelas != '') {
            $query->where('id_kelas', $request->filter_kelas);
        }

        // SHOW PER PAGE
        $perPage = $request->get('perPage', 10);

        $siswas = $query->paginate($perPage)->appends($request->all());

        // Ambil semua kelas untuk dropdown filter
        $kelass = Kelas::orderBy('tingkat')->orderBy('nama_kelas')->get();

        return view('admin.siswa.index', compact('siswas', 'kelass'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'nisn' => 'required|string|max:20|unique:siswas,nisn',
            'nama' => 'required|string|max:100',
            'tempat_lahir' => 'required|string|max:50',
            'tanggal_lahir' => 'required|date',
            'id_kelas' => 'required|exists:kelas,id_kelas',
            'password' => 'required|string|min:6',
        ], [
            'nisn.required' => 'NISN wajib diisi',
            'nisn.unique' => 'NISN sudah terdaftar',
            'nama.required' => 'Nama wajib diisi',
            'tempat_lahir.required' => 'Tempat lahir wajib diisi',
            'tanggal_lahir.required' => 'Tanggal lahir wajib diisi',
            'id_kelas.required' => 'Kelas wajib dipilih',
            'id_kelas.exists' => 'Kelas tidak valid',
            'password.required' => 'Password wajib diisi',
            'password.min' => 'Password minimal 6 karakter',
        ]);

        Siswa::create([
            'nisn' => $validated['nisn'],
            'nama' => $validated['nama'],
            'tempat_lahir' => $validated['tempat_lahir'],
            'tanggal_lahir' => $validated['tanggal_lahir'],
            'id_kelas' => $validated['id_kelas'],
            'password' => Hash::make($validated['password']),
        ]);

        return redirect()->route('admin.siswa.index')
            ->with('success', 'Data siswa berhasil ditambahkan!');
    }

    public function update(Request $request, $id)
    {
        $siswa = Siswa::findOrFail($id);

        $validated = $request->validate([
            'nama' => 'required|string|max:100',
            'tempat_lahir' => 'required|string|max:50',
            'tanggal_lahir' => 'required|date',
            'id_kelas' => 'required|exists:kelas,id_kelas',
            'password' => 'nullable|string|min:6',
        ], [
            'nama.required' => 'Nama wajib diisi',
            'tempat_lahir.required' => 'Tempat lahir wajib diisi',
            'tanggal_lahir.required' => 'Tanggal lahir wajib diisi',
            'id_kelas.required' => 'Kelas wajib dipilih',
            'id_kelas.exists' => 'Kelas tidak valid',
            'password.min' => 'Password minimal 6 karakter',
        ]);

        $siswa->nama = $validated['nama'];
        $siswa->tempat_lahir = $validated['tempat_lahir'];
        $siswa->tanggal_lahir = $validated['tanggal_lahir'];
        $siswa->id_kelas = $validated['id_kelas'];

        if ($request->filled('password')) {
            $siswa->password = Hash::make($validated['password']);
        }

        $siswa->save();

        return redirect()->route('admin.siswa.index')
            ->with('success', 'Data siswa berhasil diupdate!');
    }

    public function destroy($id)
    {
        $siswa = Siswa::findOrFail($id);
        $siswa->delete();

        return redirect()->route('admin.siswa.index')
            ->with('success', 'Data siswa berhasil dihapus!');
    }
}