<?php
// app/Http/Controllers/SiswaController.php
namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Kelas;
use App\Models\Siswa;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class SiswaController extends Controller
{
    public function index()
    {
        $kelas = Kelas::all();
        return view('siswa.index', compact('kelas'));
    }

    public function show($classId)
    {
        $siswa = Siswa::where('id_kelas', $classId)->get();
        $kelas = Kelas::find($classId);
        return view('siswa.show', compact('siswa', 'kelas'));
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nisn' => 'required|unique:siswa',
            'nama_siswa' => 'required',
            'tempat_lahir' => 'required',
            'tanggal_lahir' => 'required',
            'nomor_hp' => 'required|max:13',
            'alamat' => 'required',
            'id_kelas' => 'required',
            'password' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        try {
            DB::beginTransaction();

            $users = new User();
            $users->name = $request->nama_siswa;
            $users->username = $request->nisn;
            $users->password = $request->password;
            $users->save();

            $siswa = new Siswa();
            $siswa->nisn = $request->nisn;
            $siswa->nama_siswa = $request->nama_siswa;
            $siswa->tempat_lahir = $request->tempat_lahir;
            $siswa->tanggal_lahir = $request->tanggal_lahir;
            $siswa->nomor_hp = $request->nomor_hp;
            $siswa->alamat = $request->alamat;
            $siswa->password = $request->password;
            $siswa->id_kelas = $request->id_kelas;
            $siswa->save();

            DB::commit();

            return redirect()->back()->with('success', 'Data siswa berhasil ditambahkan.');
        } catch (\Exception $e) {
            DB::rollback();
            return back()->withErrors(['error' => $e->getMessage()]);
        }
    }


    public function update(Request $request, $nisn)
    {
        $siswa = Siswa::findOrFail($nisn);
        $siswa->update($request->except('password'));

        if ($request->filled('password')) {
            $user = User::where('username', $nisn)->first();
            if ($user) {
                $user->password = $request->password;
                $user->save();
            }
        }

        return redirect()->back()->with('success', 'Data siswa berhasil diperbarui.');
    }

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

        // Optionally, delete the related User
        User::where('username', $nisn)->delete();
        // $users->delete();

        return redirect()->back()->with('success', 'Data siswa berhasil dihapus.');
    }
}