<?php

namespace App\Http\Controllers;

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

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

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

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

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

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

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

        Ekstrakurikuler::create($data);

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

    public function edit($id_ekstrakurikuler)
    {
        $ekstrakurikuler = Ekstrakurikuler::findOrFail($id_ekstrakurikuler);
        return view('admin.edit-ekstrakurikuler', compact('ekstrakurikuler'));
    }

    public function update(Request $request, $id_ekstrakurikuler)
    {
        $ekstrakurikuler = Ekstrakurikuler::findOrFail($id_ekstrakurikuler);

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

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

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

        $ekstrakurikuler->update($data);

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

    public function destroy($id_ekstrakurikuler)
    {
        $ekstrakurikuler = Ekstrakurikuler::findOrFail($id_ekstrakurikuler);
        
        $storagePath = $this->storagePath();

        if ($ekstrakurikuler->foto && file_exists($storagePath . '/' . $ekstrakurikuler->foto)) {
            unlink($storagePath . '/' . $ekstrakurikuler->foto);
        }

        $ekstrakurikuler->delete();

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