<?php

namespace App\Http\Controllers\Api\V1\Admin;

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

class AdminSliderController extends Controller
{
    /**
     * Show the create slider page.
     */
    public function create()
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }
        // Return Inertia page for creating a slider
        return Inertia::render('Admin/Sliders/Create');
    }

    /**
     * Show the edit slider page.
     */
    public function edit($id)
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }
        $slider = Slider::findOrFail($id);
        return Inertia::render('Admin/Sliders/Edit', ['slider' => $slider]);
    }

    /**
     * List all sliders (JSON API).
     */
    public function index()
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }

        $sliders = Slider::orderBy('order', 'asc')->get();
        return response()->json($sliders);
    }

    public function store(Request $request)
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }

        $request->validate([
            'title' => 'required|string|max:255',
            'tag' => 'nullable|string|max:255',
            'subtitle' => 'nullable|string',
            'image' => 'required|image|max:2048',
            'button_text' => 'nullable|string|max:255',
            'link_url' => 'nullable|string|max:255',
            'bg_gradient' => 'nullable|string|max:255',
            'tag_color' => 'nullable|string|max:255',
            'accent_text' => 'nullable|string|max:255',
            'accent_gradient' => 'nullable|string|max:255',
            'order' => 'required|integer',
            'is_active' => 'required'
        ]);

        $imagePath = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('sliders', 'public');
        }

        $isActive = filter_var($request->is_active, FILTER_VALIDATE_BOOLEAN);

        $slider = Slider::create([
            'tag' => $request->tag,
            'title' => $request->title,
            'subtitle' => $request->subtitle,
            'image_path' => $imagePath,
            'button_text' => $request->button_text ?: 'Belanja Sekarang',
            'link_url' => $request->link_url ?: '/products',
            'bg_gradient' => $request->bg_gradient ?: 'from-slate-900 via-indigo-950 to-slate-900',
            'tag_color' => $request->tag_color ?: 'bg-indigo-500/20 text-indigo-300 border-indigo-500/30',
            'accent_text' => $request->accent_text,
            'accent_gradient' => $request->accent_gradient ?: 'from-indigo-400 via-purple-300 to-emerald-400',
            'order' => intval($request->order),
            'is_active' => $isActive,
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Slider banner created successfully.',
            'slider' => $slider
        ]);
    }

    public function update(Request $request, $id)
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }

        $slider = Slider::findOrFail($id);

        $request->validate([
            'title' => 'required|string|max:255',
            'tag' => 'nullable|string|max:255',
            'subtitle' => 'nullable|string',
            'image' => 'nullable|image|max:2048',
            'button_text' => 'nullable|string|max:255',
            'link_url' => 'nullable|string|max:255',
            'bg_gradient' => 'nullable|string|max:255',
            'tag_color' => 'nullable|string|max:255',
            'accent_text' => 'nullable|string|max:255',
            'accent_gradient' => 'nullable|string|max:255',
            'order' => 'required|integer',
            'is_active' => 'required'
        ]);

        $imagePath = $slider->image_path;
        if ($request->hasFile('image')) {
            // Delete old image if it exists and is stored locally
            if ($imagePath && !str_starts_with($imagePath, 'http') && Storage::disk('public')->exists($imagePath)) {
                Storage::disk('public')->delete($imagePath);
            }
            $imagePath = $request->file('image')->store('sliders', 'public');
        }

        $isActive = filter_var($request->is_active, FILTER_VALIDATE_BOOLEAN);

        $slider->update([
            'tag' => $request->tag,
            'title' => $request->title,
            'subtitle' => $request->subtitle,
            'image_path' => $imagePath,
            'button_text' => $request->button_text ?: 'Belanja Sekarang',
            'link_url' => $request->link_url ?: '/products',
            'bg_gradient' => $request->bg_gradient ?: 'from-slate-900 via-indigo-950 to-slate-900',
            'tag_color' => $request->tag_color ?: 'bg-indigo-500/20 text-indigo-300 border-indigo-500/30',
            'accent_text' => $request->accent_text,
            'accent_gradient' => $request->accent_gradient ?: 'from-indigo-400 via-purple-300 to-emerald-400',
            'order' => intval($request->order),
            'is_active' => $isActive,
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Slider banner updated successfully.',
            'slider' => $slider
        ]);
    }

    public function destroy($id)
    {
        if (auth()->user()->role !== 'admin') {
            return response()->json(['error' => 'Forbidden'], 403);
        }

        $slider = Slider::findOrFail($id);
        
        // Delete stored image
        if ($slider->image_path && !str_starts_with($slider->image_path, 'http') && Storage::disk('public')->exists($slider->image_path)) {
            Storage::disk('public')->delete($slider->image_path);
        }

        $slider->delete();

        return response()->json([
            'success' => true,
            'message' => 'Slider banner deleted successfully.'
        ]);
    }
}
