<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;

class SantriController extends Controller
{
    public function index()
    {
        $santri = User::all();
        return Inertia::render('list-admin/santri/IndexSantri', [
            'santri' => $santri,
            'fields' => [
                'nama' => 'text',
                'nis' => 'text',
                'password' => 'password',
                'alamat' => 'text',
                'no_telp' => 'text',
                'status_santri' => 'select',
                'jk' => 'select',
                'level' => 'select',
                'tanggal_lahir' => 'date',
                'foto' => 'file',
            ],
            'options' => [
                'status_santri' => ['lulus' => 'Lulus', 'aktif' => 'Aktif'],
                'role_santri' => ['santri' => 'Santri', 'pengurus' => 'Pengurus'],
                'jk' => ['laki laki' => 'Laki-Laki', 'perempuan' => 'Perempuan'],
                'level' => [1 => 'Admin', 2 => 'User']
            ],
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'nis' => 'required',
            'password' => 'required',
            'level' => 'required',
            'nama' => 'required',
            'alamat' => 'required',
            'status_santri' => 'required',
            'jk' => 'required',
            'tanggal_lahir' => 'required|date',
            'no_telp' => 'required',

            'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ], [
            'nis.required' => 'wajib mengisi nis santri',
            'password.required' => 'wajib mengisi password santri',
            'level.required' => 'wajib mengisi level santri',
            'nama.required' => 'wajib mengisi nama santri',
            'alamat.required' => 'wajib mengisi alamat santri',
            'status_santri.required' => 'wajib mengisi status santri',
            'jk.required' => 'wajib mengisi gender',
            'no_telp' => 'wajib mengisi no telp',
            'tanggal_lahir.required' => 'wajib mengisi tanggal lahir santri',
            'tanggal_lahir.date' => 'tanggal lahir harus dalam format tanggal yang benar',
        ]);

        $fotoPath = null;

        if ($request->hasFile('foto')) {
            $foto = $request->file('foto');
            $fotoName = time() . '_' . $foto->getClientOriginalName();
            $foto->move(public_path('fotoSantri'), $fotoName);

            $fotoPath = 'fotoSantri/' . $fotoName;
        }

        try {
            $santri = User::create([
                'nis' => $request->nis,
                'password' => Hash::make($request->password),
                'level' => $request->level,
                'nama' => $request->nama,
                'alamat' => $request->alamat,
                'status_santri' => $request->status_santri,
                'jk' => $request->jk,
                'tanggal_lahir' => $request->tanggal_lahir,
                'no_telp' => $request->no_telp,
                'foto' => $fotoPath
            ]);

            // dd($santri);
            return redirect()->back()->with('success', 'Data berhasil ditambahkan');
        } catch (\Throwable $th) {
            // dd($th->getMessage());
            return redirect()->back()->with('error', 'Data gagal ditambahkan : ' . $th->getMessage());
        }
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'nis' => 'required',
            'password' => 'nullable',
            'level' => 'required',
            'nama' => 'required',
            'alamat' => 'required',
            'status_santri' => 'required',
            'jk' => 'required',
            'tanggal_lahir' => 'required|date',
            'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ], [
            'nis.required' => 'wajib mengisi nis santri',
            'level.required' => 'wajib mengisi level santri',
            'nama.required' => 'wajib mengisi nama santri',
            'alamat.required' => 'wajib mengisi alamat santri',
            'status_santri.required' => 'wajib mengisi status santri',
            'jk.required' => 'wajib mengisi gender',
            'tanggal_lahir.required' => 'wajib mengisi tanggal lahir santri',
            'tanggal_lahir.date' => 'tanggal lahir harus dalam format tanggal yang benar',
        ]);

        try {
            $santri = User::findOrFail($id);
            $updateData = [
                'nis' => $request->nis,
                'level' => $request->level,
                'nama' => $request->nama,
                'alamat' => $request->alamat,
                'status_santri' => $request->status_santri,
                'jk' => $request->jk,
                'tanggal_lahir' => $request->tanggal_lahir,
            ];

            if ($request->hasFile('foto')) {
                if ($santri->foto && File::exists(public_path($santri->foto))) {
                    File::delete(public_path($santri->foto));
                }

                $foto = $request->file('foto');
                $fotoName = time() . '_' . $foto->getClientOriginalName();
                $foto->move(public_path('fotoSantri'), $fotoName);
                $updateData['foto'] = 'fotoSantri/' . $fotoName;
            }
            if ($request->filled('password')) {
                $updateData['password'] = Hash::make($request->password);
            }

            // return $updateData;

            $updateStatus = $santri->update($updateData);

            return redirect()->back()->with('success', 'Data Berhasil Diubah');
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', 'Gagal memperbarui data: ' . $th->getMessage());
            // return $th->getMessage();
        }
    }

    public function destroy($id)
    {
        $santri = User::findOrFail($id);

        $santri->delete();
        File::delete('fotoSantri/' . basename($santri->foto));
        return redirect()->back()->with('success', 'data berhasil di hapus');
    }
}
