<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Activity;

class ActivityController extends Controller
{
    public function index()
    {
        $activities = Activity::all();
        return view('admin.activities.index', compact('activities'));
    }

    public function create()
    {
        return view('admin.activities.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'image' => 'required|image',
            'category' => 'required',
            'description' => 'required',
        ]);

        $data = $request->only(['title', 'category', 'description']);

        if ($request->hasFile('image')) {

            $file = $request->file('image');
            $filename = time() . '.' . $file->getClientOriginalExtension();

            $destinationPath = public_path('storage/activities');

            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 0755, true);
            }

            $file->move($destinationPath, $filename);

            $data['image'] = 'activities/' . $filename;
        }

        Activity::create($data);

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

    public function edit($id)
    {
        $activity = Activity::findOrFail($id);
        return view('admin.activities.edit', compact('activity'));
    }

    public function update(Request $request, $id)
    {
        $activity = Activity::findOrFail($id);

        $request->validate([
            'title' => 'required',
            'category' => 'required',
            'description' => 'required',
        ]);

        $data = $request->only(['title', 'category', 'description']);

        if ($request->hasFile('image')) {

            // Hapus gambar lama
            if ($activity->image && file_exists(public_path('storage/' . $activity->image))) {
                unlink(public_path('storage/' . $activity->image));
            }

            $file = $request->file('image');
            $filename = time() . '.' . $file->getClientOriginalExtension();

            $destinationPath = public_path('storage/activities');

            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 0755, true);
            }

            $file->move($destinationPath, $filename);

            $data['image'] = 'activities/' . $filename;
        }

        $activity->update($data);

        return redirect()->route('activities.index')
            ->with('success', 'Kegiatan berhasil diperbarui');
    }

    public function destroy($id)
    {
        $activity = Activity::findOrFail($id);

        if ($activity->image && file_exists(public_path('storage/' . $activity->image))) {
            unlink(public_path('storage/' . $activity->image));
        }

        $activity->delete();

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