<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Slide;
use Illuminate\Http\Request;

class SlideController extends Controller
{
    // Public: returns active slides ordered by sort_order
    public function index()
    {
        $slides = Slide::where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('id')
            ->get();

        return response()->json(['data' => $slides]);
    }

    // Admin: all slides
    public function adminIndex()
    {
        $slides = Slide::orderBy('sort_order')->orderBy('id')->get();
        return response()->json(['data' => $slides]);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'                  => 'required|string|max:255',
            'subtitle'               => 'nullable|string|max:255',
            'description'            => 'nullable|string',
            'badge_text'             => 'nullable|string|max:100',
            'button_text'            => 'sometimes|string|max:100',
            'button_url'             => 'sometimes|string|max:500',
            'secondary_button_text'  => 'nullable|string|max:100',
            'secondary_button_url'   => 'nullable|string|max:500',
            'image_url'              => 'nullable|string|max:1000',
            'vial_1_url'             => 'nullable|string|max:1000',
            'vial_2_url'             => 'nullable|string|max:1000',
            'vial_3_url'             => 'nullable|string|max:1000',
            'vial_4_url'             => 'nullable|string|max:1000',
            'bg_color_from'          => 'sometimes|string|max:20',
            'bg_color_to'            => 'sometimes|string|max:20',
            'is_active'              => 'sometimes|boolean',
            'sort_order'             => 'sometimes|integer',
        ]);

        $slide = Slide::create($data);

        return response()->json(['data' => $slide], 201);
    }

    public function update(Request $request, Slide $slide)
    {
        $data = $request->validate([
            'title'                  => 'sometimes|string|max:255',
            'subtitle'               => 'nullable|string|max:255',
            'description'            => 'nullable|string',
            'badge_text'             => 'nullable|string|max:100',
            'button_text'            => 'sometimes|string|max:100',
            'button_url'             => 'sometimes|string|max:500',
            'secondary_button_text'  => 'nullable|string|max:100',
            'secondary_button_url'   => 'nullable|string|max:500',
            'image_url'              => 'nullable|string|max:1000',
            'vial_1_url'             => 'nullable|string|max:1000',
            'vial_2_url'             => 'nullable|string|max:1000',
            'vial_3_url'             => 'nullable|string|max:1000',
            'vial_4_url'             => 'nullable|string|max:1000',
            'bg_color_from'          => 'sometimes|string|max:20',
            'bg_color_to'            => 'sometimes|string|max:20',
            'is_active'              => 'sometimes|boolean',
            'sort_order'             => 'sometimes|integer',
        ]);

        $slide->update($data);

        return response()->json(['data' => $slide]);
    }

    public function destroy(Slide $slide)
    {
        $slide->delete();
        return response()->json(['message' => 'Slide deleted']);
    }

    // Bulk reorder: [{id, sort_order}, ...]
    public function reorder(Request $request)
    {
        $request->validate(['order' => 'required|array', 'order.*.id' => 'required|integer', 'order.*.sort_order' => 'required|integer']);
        foreach ($request->order as $item) {
            Slide::where('id', $item['id'])->update(['sort_order' => $item['sort_order']]);
        }
        return response()->json(['message' => 'Reordered']);
    }
}
