<?php

namespace App\Http\Controllers;

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

class ExhibitController extends Controller
{
    // List all exhibits
    public function index()
    {
        $exhibits = Exhibit::with('gallery')->get();
        return view('exhibits.index', compact('exhibits'));
    }

    // Show create exhibit form
    public function create()
    {
        $galleries = Gallery::all();
        $exhibits = Exhibit::all();
        return view('exhibits.create', compact('galleries','exhibits'));
    }

    // Store a new exhibit
    public function store(Request $request)
    {
        /*$request->validate([
            'gallery_id' => 'required|exists:galleries,id',
            'exhibit_name' => 'required|string|max:255',
            'exhibit_icon_path' => 'nullable|image|mimes:jpg,png,jpeg|max:2048',
            'short_description' => 'nullable|string',
            'language' => 'required',
            'auto_play' => 'required',
            'order' => 'required|integer',
            'video_path' => 'nullable|file|mimes:mp4',
            'show_description_flag' => 'required',
            'next_exhibit_ids' => 'nullable',
        ]);*/

        $request->validate([
            'gallery_id' => 'required|exists:galleries,id',
            'exhibit_name' => 'required|string|max:255',
            'exhibit_icon_path' => 'nullable|string',
            'short_description' => 'nullable|string',
            'language' => 'required',
            'auto_play' => 'required',
            'order' => 'required|integer',
            'video_path' => 'nullable|string',
            'show_description_flag' => 'required',
            'close_after_autoplay' => 'required|in:Yes,No',
            'show_in_app_icon' => 'required|in:Yes,No',
            'next_exhibit_id' => 'nullable|exists:exhibits,id',
            'previous_exhibit_id' => 'nullable|exists:exhibits,id',
        ]);

        $data = $request->all();

        /*if ($request->hasFile('exhibit_icon_path')) {
            $data['exhibit_icon_path'] = $request->file('exhibit_icon_path')->store('exhibit_icons', 'public');
        }

        if ($request->hasFile('video_path')) {
            $data['video_path'] = $request->file('video_path')->store('videos', 'public');
        }*/

        Exhibit::create($data);

        return redirect()->route('exhibits.index')->with('success', 'Exhibit added successfully.');
    }

    // Show edit exhibit form
    public function edit($id)
    {
        $exhibit = Exhibit::findOrFail($id);
        $galleries = Gallery::all();
        $exhibits = Exhibit::where('id', '!=', $id)->where('gallery_id',$exhibit->gallery_id)->get();
        return view('exhibits.edit', compact('exhibit', 'galleries','exhibits'));
    }

    // Update exhibit
    public function update(Request $request, $id)
    {
        $exhibit = Exhibit::findOrFail($id);

        /*$request->validate([
            'gallery_id' => 'required|exists:galleries,id',
            'exhibit_name' => 'required|string|max:255',
            'exhibit_icon_path' => 'nullable|image|mimes:jpg,png,jpeg|max:2048',
            'short_description' => 'nullable|string',
            'language' => 'required',
            'auto_play' => 'required',
            'order' => 'required|integer',
            'video_path' => 'nullable|file|mimes:mp4',
            'show_description_flag' => 'required',
            'next_exhibit_ids' => 'nullable',
        ]);*/

        $request->validate([
            'gallery_id' => 'required|exists:galleries,id',
            'exhibit_name' => 'required|string|max:255',
            'exhibit_icon_path' => 'nullable|string',
            'short_description' => 'nullable|string',
            'language' => 'required',
            'auto_play' => 'required',
            'order' => 'required|integer',
            'video_path' => 'nullable|string',
            'show_description_flag' => 'required',
            'close_after_autoplay' => 'required|in:Yes,No',
            'show_in_app_icon' => 'required|in:Yes,No',
            'next_exhibit_id' => 'nullable|exists:exhibits,id',
            'previous_exhibit_id' => 'nullable|exists:exhibits,id',

        ]);

        $data = $request->all();

        /*if ($request->hasFile('exhibit_icon_path')) {
            if ($exhibit->exhibit_icon_path) {
                Storage::disk('public')->delete($exhibit->exhibit_icon_path);
            }
            $data['exhibit_icon_path'] = $request->file('exhibit_icon_path')->store('exhibit_icons', 'public');
        }

        if ($request->hasFile('video_path')) {
            if ($exhibit->video_path) {
                Storage::disk('public')->delete($exhibit->video_path);
            }
            $data['video_path'] = $request->file('video_path')->store('videos', 'public');
        }*/

        $exhibit->update($data);

        return redirect()->route('exhibits.index')->with('success', 'Exhibit updated successfully.');
    }

    // Delete exhibit
    public function destroy($id)
    {
        $exhibit = Exhibit::findOrFail($id);
        
        if ($exhibit->exhibit_icon_path) {
            Storage::disk('public')->delete($exhibit->exhibit_icon_path);
        }
        
        if ($exhibit->video_path) {
            Storage::disk('public')->delete($exhibit->video_path);
        }

        $exhibit->delete();

        return redirect()->route('exhibits.index')->with('success', 'Exhibit deleted successfully.');
    }
}
