<?php

namespace App\Http\Controllers;

use App\Models\Gallery;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GalleryController extends Controller
{
    public function index()
    {
        $galleries = Gallery::orderBy('created_at', 'desc')->get();
        return response()->json($galleries);
    }

    public function create()
    {
        return response()->json(['message' => 'Not implemented in API'], 404);
    }

    public function store(Request $request)
    {
        $request->validate([
            'media' => 'required|file|mimes:jpeg,png,jpg,webp,mp4,mov,avi|max:51200', // max 50MB
            'title' => 'nullable|string|max:255',
            'category' => 'nullable|string|max:255',
        ]);

        $file = $request->file('media');
        $mime = $file->getMimeType();
        $type = str_contains($mime, 'video') ? 'video' : 'image';

        $path = $file->store('galleries', 'public');

        $gallery = Gallery::create([
            'title' => $request->title,
            'type' => $type,
            'media_path' => '/storage/' . $path,
            'category' => $request->category,
            'is_active' => $request->has('is_active') ? true : false,
        ]);

        return response()->json($gallery, 201);
    }

    public function edit(Gallery $gallery)
    {
        return response()->json($gallery);
    }

    public function update(Request $request, Gallery $gallery)
    {
        $request->validate([
            'media' => 'nullable|file|mimes:jpeg,png,jpg,webp,mp4,mov,avi|max:51200',
            'title' => 'nullable|string|max:255',
            'category' => 'nullable|string|max:255',
        ]);

        if ($request->hasFile('media')) {
            // Delete old
            $oldPath = str_replace('/storage/', '', $gallery->media_path);
            if (Storage::disk('public')->exists($oldPath)) {
                Storage::disk('public')->delete($oldPath);
            }

            $file = $request->file('media');
            $mime = $file->getMimeType();
            $gallery->type = str_contains($mime, 'video') ? 'video' : 'image';
            $path = $file->store('galleries', 'public');
            $gallery->media_path = '/storage/' . $path;
        }

        $gallery->title = $request->title;
        $gallery->category = $request->category;
        $gallery->is_active = $request->has('is_active') ? true : false;
        $gallery->save();

        return response()->json($gallery);
    }

    public function destroy(Gallery $gallery)
    {
        $oldPath = str_replace('/storage/', '', $gallery->media_path);
        if (Storage::disk('public')->exists($oldPath)) {
            Storage::disk('public')->delete($oldPath);
        }
        $gallery->delete();

        return response()->json(['message' => 'Media deleted successfully']);
    }

    // API function
    public function getActiveGalleries()
    {
        $galleries = Gallery::where('is_active', true)->orderBy('created_at', 'desc')->get();
        return response()->json($galleries);
    }
}
