<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\GalleryItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GalleryController extends Controller
{
    public function index()
    {
        $items = GalleryItem::latest()->get();
        return view('admin.gallery.index', compact('items'));
    }

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

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'nullable|string|max:255',
            'type' => 'required|in:image,youtube,tiktok',
            'is_active' => 'boolean',
        ]);

        $data = $request->except(['image_file']);
        $data['is_active'] = $request->has('is_active');
        $data['show_on_homepage'] = $request->has('show_on_homepage');

        if ($request->type === 'image') {
            $request->validate(['image_file' => 'required|image|max:5120']);
            $path = $request->file('image_file')->store('gallery', 'public');
            $data['media_url'] = '/storage/' . $path;
        } else {
            $request->validate(['media_url' => 'required|string']);
            // For video, media_url from input is used
        }

        GalleryItem::create($data);

        return redirect()->route('admin.gallery.index')->with('success', 'Gallery item created successfully.');
    }

    public function edit(GalleryItem $gallery)
    {
        return view('admin.gallery.edit', compact('gallery'));
    }

    public function update(Request $request, GalleryItem $gallery)
    {
        $request->validate([
            'title' => 'nullable|string|max:255',
            'type' => 'required|in:image,youtube,tiktok',
            'is_active' => 'boolean',
        ]);

        $data = $request->except(['image_file']);
        $data['is_active'] = $request->has('is_active');
        $data['show_on_homepage'] = $request->has('show_on_homepage');

        if ($request->type === 'image') {
            if ($request->hasFile('image_file')) {
                $request->validate(['image_file' => 'image|max:5120']);
                // Delete old image if exists
                if ($gallery->type === 'image' && $gallery->media_url) {
                    $oldPath = str_replace('/storage/', '', $gallery->media_url);
                    Storage::disk('public')->delete($oldPath);
                }
                $path = $request->file('image_file')->store('gallery', 'public');
                $data['media_url'] = '/storage/' . $path;
            } else {
                // Keep existing media_url if no new image is uploaded but type is still image
                $data['media_url'] = $gallery->media_url;
            }
        } else {
            $request->validate(['media_url' => 'required|string']);
        }

        $gallery->update($data);

        return redirect()->route('admin.gallery.index')->with('success', 'Gallery item updated successfully.');
    }

    public function destroy(GalleryItem $gallery)
    {
        if ($gallery->type === 'image' && $gallery->media_url) {
            $oldPath = str_replace('/storage/', '', $gallery->media_url);
            Storage::disk('public')->delete($oldPath);
        }
        
        $gallery->delete();

        return redirect()->route('admin.gallery.index')->with('success', 'Gallery item deleted successfully.');
    }
}
