<?php

namespace App\Http\Controllers;

use App\Models\Informasi;
use App\Models\Penyakit;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Redirect;

class InformasiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $numtab = 1;
        $informasi = Informasi::with(['penyakit'])->latest()->get();
        return view('server-side.pages.manajemen-konten.informasi.data', compact([
            'numtab', 'informasi'
        ]));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $penyakit = Penyakit::all();
        return view('server-side.pages.manajemen-konten.informasi.create', compact('penyakit'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'judul' => ['required'],
            'penyakitId' => ['required'],
            'gambar' => ['required', 'image', 'mimes:png,jpg,jpeg', 'max:2048'],
            'artikel' => ['required'],
        ]);

        $data = $request->all();
        $gambar = $data['gambar'];
        $filename = strtolower(time() . '-' . $gambar->getClientOriginalName());
        $destinationPath = public_path('assets/images/informasi');
        $gambar->move($destinationPath, $filename);
        $data['gambar'] = strtolower(time() . '-' . $gambar->getClientOriginalName());

        Informasi::create([
            'penyakitId' => $data['penyakitId'],
            'judul' => ucwords($data['judul']),
            'slug' => Str::slug($data['judul']),
            'gambar' => $data['gambar'],
            'penulis' => Auth::user()->name,
            'artikel' => $data['artikel'],
        ]);
        return Redirect::route('informasi.index')->with('message', 'Berhasil menambah data informasi baru');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Informasi  $informasi
     * @return \Illuminate\Http\Response
     */
    public function show(Informasi $informasi)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Informasi  $informasi
     * @return \Illuminate\Http\Response
     */
    public function edit(Informasi $informasi)
    {
        $detail = Informasi::where('id', $informasi->id)->first();
        $penyakit = Penyakit::all();
        return view('server-side.pages.manajemen-konten.informasi.edit', compact('informasi', 'detail', 'penyakit'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Informasi  $informasi
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Informasi $informasi)
    {
        $validatedData = $request->validate([
            'judul' => ['required'],
            'penyakitId' => ['required'],
            'gambar' => $request->hasFile('gambar') ? ['image', 'mimes:png,jpg,jpeg', 'max:2048'] : ['nullable'],
            'artikel' => ['required'],
        ]);

        $data = $request->all();

        if ($request->hasFile('gambar')) {
            if ($informasi->gambar) {
                $oldImagePath = public_path('assets/images/informasi/' . $informasi->gambar);
                if (File::exists($oldImagePath)) {
                    File::delete($oldImagePath);
                }
            }
            $gambar = $request->file('gambar');
            $filename = strtolower(time() . '-' . $gambar->getClientOriginalName());
            $destinationPath = public_path('assets/images/informasi');
            $gambar->move($destinationPath, $filename);
            $data['gambar'] = $filename;
        } else {
            $data['gambar'] = $request->input('gambar_lama');
        }

        Informasi::find($informasi->id)->update([
            'penyakitId' => $data['penyakitId'],
            'judul' => ucwords($data['judul']),
            'slug' => Str::slug($data['judul']),
            'gambar' => $data['gambar'],
            'penulis' => Auth::user()->name,
            'artikel' => $data['artikel'],
        ]);

        return redirect()->route('informasi.index')->with('message', 'Berhasil memperbarui data informasi yang dipilih');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Informasi  $informasi
     * @return \Illuminate\Http\Response
     */
    public function destroy(Informasi $informasi)
    {
        if ($informasi->gambar) {
            $imagePath = public_path('assets/images/informasi/' . $informasi->gambar);
            if (File::exists($imagePath)) {
                File::delete($imagePath);
            }
        }
        $informasi->delete();
        return redirect()->route('informasi.index')->with('message', 'Berhasil menghapus data informasi yang dipilih');
    }
}
