<?php

namespace App\Http\Controllers\Partner;

use App\Http\Controllers\Controller;
use App\Models\Event;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class EventController extends Controller
{
    public function index()
    {
        $partner = Auth::guard('partner')->user();
        $events = Event::where('partner_id', $partner->id)->latest()->get();

        return view('partner.events.index', compact('events'));
    }

    public function create()
    {
        $categories = Category::all();
        return view('partner.events.create', compact('categories'));
    }

    public function store(Request $request)
    {
        $partner = Auth::guard('partner')->user();

        $request->validate([
            'category_id' => 'required|exists:categories,id',
            'title' => 'required|string|max:255',
            'description' => 'required',
            'date' => 'required|date',
            'location' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'stock' => 'required|numeric|min:0',
            'poster' => 'nullable|image|max:2048',
        ]);

        $posterPath = null;
        if ($request->hasFile('poster')) {
            $posterPath = $request->file('poster')->store('posters', 'public');
        }

        Event::create([
            'partner_id' => $partner->id,
            'category_id' => $request->category_id,
            'title' => $request->title,
            'description' => $request->description,
            'date' => $request->date,
            'location' => $request->location,
            'price' => $request->price,
            'stock' => $request->stock,
            'poster_path' => $posterPath,
        ]);

        return redirect()->route('partner.events.index')
            ->with('success', 'Event berhasil dibuat!');
    }

    public function edit(Event $event)
    {
        $partner = Auth::guard('partner')->user();

        if ($event->partner_id !== $partner->id) {
            abort(403);
        }

        $categories = Category::all();
        return view('partner.events.edit', compact('event', 'categories'));
    }

    public function update(Request $request, Event $event)
    {
        $partner = Auth::guard('partner')->user();

        if ($event->partner_id !== $partner->id) {
            abort(403);
        }

        $request->validate([
            'category_id' => 'required|exists:categories,id',
            'title' => 'required|string|max:255',
            'description' => 'required',
            'date' => 'required|date',
            'location' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'stock' => 'required|numeric|min:0',
            'poster' => 'nullable|image|max:2048',
        ]);

        $data = $request->only(['category_id', 'title', 'description', 'date', 'location', 'price', 'stock']);

        if ($request->hasFile('poster')) {
            $data['poster_path'] = $request->file('poster')->store('posters', 'public');
        }

        $event->update($data);

        return redirect()->route('partner.events.index')
            ->with('success', 'Event berhasil diperbarui!');
    }

    public function destroy(Event $event)
    {
        $partner = Auth::guard('partner')->user();

        if ($event->partner_id !== $partner->id) {
            abort(403);
        }

        $event->delete();
        return redirect()->route('partner.events.index')
            ->with('success', 'Event berhasil dihapus.');
    }
}

