<?php

namespace App\Http\Controllers;

use App\Models\Event;
use App\Models\EventImage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class EventImageController extends Controller
{
    public function store(Request $request, Event $event)
    {
        $this->authorize('upload', $event); // ✅ invitados con código o dueño

        $request->validate([
            'images.*' => ['required','image','mimes:jpg,jpeg,png,webp','max:8192'],
        ]);

        foreach ($request->file('images', []) as $file) {
            $path = $file->store('event_images', 'public');

            $event->images()->create([
                'path'          => $path,
                'original_name' => $file->getClientOriginalName(),
                'order'         => $event->images()->count(),
            ]);
        }

        return back()->with('ok','Imágenes subidas');
    }

    public function destroy(Event $event, EventImage $image)
    {
        $this->authorize('update', $event); // ❗ solo el dueño elimina

        abort_unless($image->event_id === $event->id, 404);

        Storage::disk('public')->delete($image->path);
        $image->delete();

        return back()->with('ok','Imagen eliminada');
    }

    public function reorder(Request $request, Event $event)
    {
        $this->authorize('update', $event); // ❗ solo el dueño reordena
        // ...
    }

}
