<?php

namespace App\Http\Controllers;

use App\Enums\JenisPengaduan;
use App\Models\Guru;
use App\Models\Pelanggaran;
use App\Models\Pengaduan;
use App\Models\Siswa;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rules\Enum;

class PengaduanController extends Controller
{
    public function index()
    {
        if (Auth::check()) {
            foreach (Auth::user()->roles as $role) {
                if ($role->name == 'Siswa') {
                    $userId = Auth::id();
                    $siswa = Siswa::where('id_user', $userId)->first();

                    if ($siswa) {
                        $pengaduans = Pengaduan::where('id_user', $siswa->id_user)->get();
                    } else {
                        $pengaduans = collect();
                    }
                } else {
                    $pengaduans = Pengaduan::with('user')->get();
                }
            }
        }
        return view('pengaduan.index', compact('pengaduans'));
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        $userId = Auth::id();

        // dd($siswaId);

        $request->validate([
            'jenis_pengaduan' => ['required', 'max:50', new Enum(JenisPengaduan::class)],
            'metode' => 'required|max:25',
            'isi_keluhan' => 'required|string',
            'dokumentasi' => 'nullable|image|mimes:jpg,jpeg,png,pdf',
        ]);

        $dokumentasi = null;
        if ($request->hasFile('dokumentasi')) {
            $path = $request->file('dokumentasi')->storeAs(
                'dokumentasi',
                'dokumentasi_' . time() . '.' . $request->file('dokumentasi')->extension(),
                'public'
            );
            $dokumentasi = basename($path);
        }

        $tanggal = date('Y-m-d');

        Pengaduan::create([
            'id_user' => $userId,
            'tanggal' => $tanggal,
            'jenis_pengaduan' => $request->jenis_pengaduan,
            'metode' => $request->metode,
            'judul_pengaduan' => $request->judul_pengaduan,
            'isi_keluhan' => $request->isi_keluhan,
            'dokumentasi' => $dokumentasi ?? null,
            'status' => 0,
        ]);

        $notification = array(
            'message' => 'Data pengaduan berhasil dikirim!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    public function show(string $jenis)
    {
        $validatedJenis = collect(JenisPengaduan::cases())->pluck('value')->contains($jenis)
            ? $jenis
            : abort(404);

        $pengaduans = Pengaduan::where('jenis_pengaduan', $validatedJenis)->get();

        return view('pengaduan.index', compact('pengaduans', 'validatedJenis'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id_pengaduan_bk)
    {
        $pengaduan = Pengaduan::findOrFail($id_pengaduan_bk);

        $userId = Auth::id();
        $siswa = Siswa::where('id_user', $userId)->firstOrFail();
        $idUser = $siswa->id_user;

        // dd($siswaId);

        $request->validate([
            'jenis_pengaduan' => ['required', 'max:50', new Enum(JenisPengaduan::class)],
            'metode' => 'required|max:25',
            'isi_keluhan' => 'required|string',
            'dokumentasi' => 'nullable|image|mimes:jpg,jpeg,png,pdf',
        ]);

        $dokumentasi = null;
        if ($request->hasFile('dokumentasi')) {
            $path = $request->file('dokumentasi')->storeAs(
                'dokumentasi',
                'dokumentasi_' . time() . '.' . $request->file('dokumentasi')->extension(),
                'public'
            );
            $dokumentasi = basename($path);
        }

        $tanggal = date('Y-m-d');

        $pengaduan->update([
            'id_user' => $idUser,
            'tanggal' => $tanggal,
            'jenis_pengaduan' => $request->jenis_pengaduan,
            'metode' => $request->metode,
            'judul_pengaduan' => $request->judul_pengaduan,
            'isi_keluhan' => $request->isi_keluhan,
            'dokumentasi' => $dokumentasi ?? null,
            'status' => 0,
        ]);

        $notification = array(
            'message' => 'Data pengaduan berhasil diubah!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_pengaduan_bk)
    {
        $pengaduan = Pengaduan::find($id_pengaduan_bk);

        $pengaduan->delete();

        $notification = array(
            'message' => 'Data pengaduan berhasil dihapus!',
            'alert-type' => 'success'
        );

        return redirect()->route('dashboard')->with($notification);
    }

    public function verify($id_pengaduan_bk)
    {
        $pengaduan = Pengaduan::find($id_pengaduan_bk);

        $pengaduan->status = 1;
        $pengaduan->save();

        $notification = [
            'message' => "pengaduan berhasil diverifikasi!",
            'alert-type' => 'success'
        ];

        return redirect()->route('dashboard')->with($notification);
    }

    public function unverify($id_pengaduan_bk)
    {
        $pengaduan = Pengaduan::find($id_pengaduan_bk);

        $pengaduan->status = 0;
        $pengaduan->save();

        $notification = [
            'message' => "Pengaduan berhasil diunverifikasi!",
            'alert-type' => 'success'
        ];

        return redirect()->route('dashboard')->with($notification);
    }
}
