<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Fasilitas;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;

class FasilitasController extends Controller
{
    protected function storagePath()
    {
        return env('STORAGE_PATH', public_path('penyimpanan'));
    }

    public function index()
    {
        $data = Fasilitas::orderBy('id_fasilitas', 'desc')->paginate(15);
        return view('admin.fasilitas', compact('data'));
    }

    public function create()
    {
        return view('admin.form-fasilitas');
    }

    public function store(Request $request)
    {
        $request->validate([
            'nama_fasilitas' => 'required|string|max:255',
            'keterangan' => 'nullable|string',
            'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
        ]);

        $data = $request->all();
        $data['id_user'] = Auth::id();
        
        $storagePath = $this->storagePath();

        // Handle upload foto
        if ($request->hasFile('foto')) {
            if (!file_exists($storagePath . '/foto_fasilitas')) {
                mkdir($storagePath . '/foto_fasilitas', 0755, true);
            }
            $filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
            $request->file('foto')->move($storagePath . '/foto_fasilitas', $filename);
            $data['foto'] = 'foto_fasilitas/' . $filename;
        }

        Fasilitas::create($data);

        return redirect()->route('fasilitas.index')->with('success', 'Data berhasil ditambahkan');
    }

    public function edit($id_fasilitas)
    {
        $fasilitas = Fasilitas::findOrFail($id_fasilitas);
        return view('admin.edit-fasilitas', compact('fasilitas'));
    }

    public function update(Request $request, $id_fasilitas)
    {
        $fasilitas = Fasilitas::findOrFail($id_fasilitas);

        $request->validate([
            'nama_fasilitas' => 'required|string|max:255',
            'keterangan' => 'nullable|string',
            'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
        ]);

        $data = $request->all();
        $storagePath = $this->storagePath();

        // Handle upload foto baru
        if ($request->hasFile('foto')) {
            // Hapus foto lama jika ada
            if ($fasilitas->foto && file_exists($storagePath . '/' . $fasilitas->foto)) {
                unlink($storagePath . '/' . $fasilitas->foto);
            }
            
            if (!file_exists($storagePath . '/foto_fasilitas')) {
                mkdir($storagePath . '/foto_fasilitas', 0755, true);
            }
            $filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
            $request->file('foto')->move($storagePath . '/foto_fasilitas', $filename);
            $data['foto'] = 'foto_fasilitas/' . $filename;
        } else {
            unset($data['foto']);
        }

        $fasilitas->update($data);

        return redirect()->route('fasilitas.index')->with('success', 'Data berhasil diupdate');
    }

    public function destroy($id_fasilitas)
    {
        $fasilitas = Fasilitas::findOrFail($id_fasilitas);
        
        $storagePath = $this->storagePath();
        if ($fasilitas->foto && file_exists($storagePath . '/' . $fasilitas->foto)) {
            unlink($storagePath . '/' . $fasilitas->foto);
        }
        
        $fasilitas->delete();
        return redirect()->route('fasilitas.index')->with('success', 'Data berhasil dihapus');
    }
}
