<?php

namespace App\Http\Controllers;

use App\Models\Psikolog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PsikologController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['psikolog'] = Psikolog::all();
        return view('psikolog.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validate = $request->validate([
            'tanggal' => 'required|date',
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'kesimpulan_sementara' => 'required|max:255',
            'penanganan' => 'required|max:255',
            'rekomendasi_lanjut' => 'nullable|file|mimes:pdf',
        ]);

        // dd($validate);

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

        Psikolog::create([
            'tanggal' => $validate['tanggal'],
            'id_siswa' => $validate['id_siswa'],
            'kesimpulan_sementara' => $validate['kesimpulan_sementara'],
            'penanganan' => $validate['penanganan'],
            'rekomendasi_lanjut' => $rekomendasi_lanjut ?? null,
            'status' => 0,
        ]);

        $notification = [
            'message' => 'Data penanganan berhasil ditambah!',
            'alert-type' => 'success'
        ];

        return redirect()->route('psikolog.index')->with($notification);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * 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_psikolog)
    {
        $psikolog = Psikolog::findOrFail($id_psikolog);

        // dd($siswaId);

        $validate = $request->validate([
            'tanggal' => 'required|date',
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'kesimpulan_sementara' => 'required|max:255',
            'penanganan' => 'required|max:255',
            'rekomendasi_lanjut' => 'nullable|file|mimes:pdf',
        ]);

        if ($request->hasFile('rekomendasi_lanjut')) {
            if ($psikolog->rekomendasi_lanjut && Storage::exists('public/rekomendasi_lanjut/' . $psikolog->rekomendasi_lanjut)) {
                Storage::delete('public/rekomendasi_lanjut/' . $psikolog->rekomendasi_lanjut);
            }
            $file = $request->file('rekomendasi_lanjut');
            $filename = time() . '_' . $file->getClientOriginalName();
            $file->storeAs('public/rekomendasi_lanjut', $filename);

            $psikolog['rekomendasi_lanjut'] = $filename;
        }

        $psikolog->update([
            'tanggal' => $validate['tanggal'],
            'id_siswa' => $validate['id_siswa'],
            'kesimpulan_sementara' => $validate['kesimpulan_sementara'],
            'penanganan' => $validate['penanganan'],
            'status' => 0,
        ]);

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

        return redirect()->route('psikolog.index')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_psikolog)
    {
        $psikolog = Psikolog::find($id_psikolog);

        $psikolog->delete();

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

        return redirect()->route('psikolog.index')->with($notification);
    }

    public function verify($id_psikolog)
    {
        $psikolog = Psikolog::find($id_psikolog);

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

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

        return redirect()->route('psikolog.index')->with($notification);
    }

    public function unverify($id_psikolog)
    {
        $psikolog = Psikolog::find($id_psikolog);

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

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

        return redirect()->route('psikolog.index')->with($notification);
    }
}
