<?php

namespace App\Http\Controllers\Admin;

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

class GalleryController extends Controller
{
    public function index()
    {
        $categories = GalleryCategory::with('images')->get();
        return view('admin.gallery.index', compact('categories'));
    }

    public function storeCategory(Request $request)
    {
        try {
            $request->validate([
                'name' => 'required|string|max:255',
                'description' => 'required|string',
                'thumbnail' => 'required|image|mimes:jpeg,png,jpg|max:2048'
            ]);

            $thumbnailPath = $request->file('thumbnail')->store('gallery/thumbnails', 'public');

            $category = GalleryCategory::create([
                'name' => $request->name,
                'description' => $request->description,
                'thumbnail' => $thumbnailPath
            ]);

            return response()->json([
                'success' => true,
                'message' => 'Kategori galeri berhasil ditambahkan',
                'data' => $category
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal menambahkan kategori: ' . $e->getMessage()
            ], 500);
        }
    }

    public function updateCategory(Request $request, GalleryCategory $category)
    {
        try {
            $request->validate([
                'name' => 'required|string|max:255',
                'description' => 'required|string',
                'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
            ]);

            $data = [
                'name' => $request->name,
                'description' => $request->description
            ];

            if ($request->hasFile('thumbnail')) {
                if ($category->thumbnail) {
                    Storage::disk('public')->delete($category->thumbnail);
                }
                $data['thumbnail'] = $request->file('thumbnail')->store('gallery/thumbnails', 'public');
            }

            $category->update($data);

            return response()->json([
                'success' => true,
                'message' => 'Kategori galeri berhasil diperbarui',
                'data' => $category
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal memperbarui kategori: ' . $e->getMessage()
            ], 500);
        }
    }

    public function destroyCategory(GalleryCategory $category)
    {
        try {
            // Delete thumbnail
            if ($category->thumbnail) {
                Storage::disk('public')->delete($category->thumbnail);
            }

            // Delete all related images
            foreach ($category->images as $image) {
                Storage::disk('public')->delete($image->image);
            }

            $category->delete();

            return response()->json([
                'success' => true,
                'message' => 'Kategori galeri berhasil dihapus'
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal menghapus kategori: ' . $e->getMessage()
            ], 500);
        }
    }

    public function storeImage(Request $request, GalleryCategory $category)
    {
        try {
            $request->validate([
                'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
                'caption' => 'nullable|string|max:255'
            ]);

            $imagePath = $request->file('image')->store('gallery/images', 'public');

            $image = $category->images()->create([
                'image' => $imagePath,
                'caption' => $request->caption
            ]);

            return response()->json([
                'success' => true,
                'message' => 'Gambar berhasil ditambahkan ke galeri',
                'data' => $image
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal menambahkan gambar: ' . $e->getMessage()
            ], 500);
        }
    }

    public function updateImage(Request $request, GalleryImage $image)
    {
        try {
            $request->validate([
                'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
                'caption' => 'nullable|string|max:255'
            ]);

            $data = [];
            
            if ($request->hasFile('image')) {
                Storage::disk('public')->delete($image->image);
                $data['image'] = $request->file('image')->store('gallery/images', 'public');
            }

            if ($request->has('caption')) {
                $data['caption'] = $request->caption;
            }

            $image->update($data);

            return response()->json([
                'success' => true,
                'message' => 'Gambar berhasil diperbarui',
                'data' => $image
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal memperbarui gambar: ' . $e->getMessage()
            ], 500);
        }
    }

    public function destroyImage(GalleryImage $image)
    {
        try {
            if ($image->image) {
                Storage::disk('public')->delete($image->image);
            }

            $image->delete();

            return response()->json([
                'success' => true,
                'message' => 'Gambar berhasil dihapus dari galeri'
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal menghapus gambar: ' . $e->getMessage()
            ], 500);
        }
    }

    public function getCategory(GalleryCategory $category)
    {
        try {
            return response()->json([
                'success' => true,
                'data' => $category
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch category data: ' . $e->getMessage()
            ], 500);
        }
    }

    public function getImage(GalleryImage $image)
    {
        try {
            return response()->json([
                'success' => true,
                'data' => $image
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to fetch image data: ' . $e->getMessage()
            ], 500);
        }
    }
} 