<?php

namespace App\Http\Controllers;

use App\Models\Event;
use Illuminate\Http\Request;

class EventManagementController extends Controller
{
    /**
     * Alle Events des angemeldeten Users auflisten.
     */
    public function myEvents(Request $request)
    {
        $events = Event::where('created_by', $request->user()->id)
            ->with(['location', 'occurrences'])
            ->orderBy('created_at', 'desc')
            ->get();

        return response()->json([
            'success' => true,
            'data' => $events,
        ]);
    }

    /**
     * Neues Event erstellen (nur für Organizer).
     */
    public function create(Request $request)
    {
        // Prüfe ob User ein Organizer ist
        if (!$request->user()->isOrganizer()) {
            return response()->json([
                'success' => false,
                'message' => 'Nur Organizer können Events erstellen',
            ], 403);
        }

        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'sometimes|string',
            'category' => 'sometimes|string|max:100',
            'location_id' => 'sometimes|exists:locations,id',
            'contact_email' => 'sometimes|email',
            'contact_phone' => 'sometimes|string',
            'image_url' => 'sometimes|url',
            'website_url' => 'sometimes|url',
        ]);

        $event = Event::create([
            ...$validated,
            'created_by' => $request->user()->id,
            'status' => 'draft',
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Event erstellt',
            'data' => $event,
        ], 201);
    }

    /**
     * Event aktualisieren.
     */
    public function update(Request $request, Event $event)
    {
        // Prüfe ob User der Creator ist
        if ($event->created_by !== $request->user()->id && !$request->user()->isAdmin()) {
            return response()->json([
                'success' => false,
                'message' => 'Nicht berechtigt dieses Event zu bearbeiten',
            ], 403);
        }

        $validated = $request->validate([
            'title' => 'sometimes|string|max:255',
            'description' => 'sometimes|string',
            'category' => 'sometimes|string|max:100',
            'location_id' => 'sometimes|exists:locations,id',
            'contact_email' => 'sometimes|email',
            'contact_phone' => 'sometimes|string',
            'image_url' => 'sometimes|url',
            'website_url' => 'sometimes|url',
            'status' => 'sometimes|in:draft,published,archived',
        ]);

        $event->update($validated);

        return response()->json([
            'success' => true,
            'message' => 'Event aktualisiert',
            'data' => $event,
        ]);
    }

    /**
     * Event löschen.
     */
    public function delete(Request $request, Event $event)
    {
        // Prüfe ob User der Creator ist
        if ($event->created_by !== $request->user()->id && !$request->user()->isAdmin()) {
            return response()->json([
                'success' => false,
                'message' => 'Nicht berechtigt dieses Event zu löschen',
            ], 403);
        }

        $event->delete();

        return response()->json([
            'success' => true,
            'message' => 'Event gelöscht',
        ]);
    }

    /**
     * Event als Favorit hinzufügen/entfernen.
     */
    public function toggleFavorite(Request $request, Event $event)
    {
        $user = $request->user();

        if ($user->favoriteEvents()->where('event_id', $event->id)->exists()) {
            $user->favoriteEvents()->detach($event->id);
            $isFavorited = false;
            $message = 'Event aus Favoriten entfernt';
        } else {
            $user->favoriteEvents()->attach($event->id);
            $isFavorited = true;
            $message = 'Event zu Favoriten hinzugefügt';
        }

        return response()->json([
            'success' => true,
            'message' => $message,
            'is_favorited' => $isFavorited,
        ]);
    }

    /**
     * Alle Favoriten des Users abrufen.
     */
    public function favorites(Request $request)
    {
        $favorites = $request->user()->favoriteEvents()
            ->with(['location', 'creator', 'occurrences'])
            ->get();

        return response()->json([
            'success' => true,
            'data' => $favorites,
        ]);
    }
}
