<?php

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

use App\Http\Controllers\Api\V1\FlashSale\Resources\FlashSaleResource;
use App\Http\Controllers\Controller;
use App\Models\CMS\Marketing\FlashSale;
use Illuminate\Http\JsonResponse;

class FlashSaleController extends Controller
{
    /**
     * Active flash sales.
     */
    public function index(): JsonResponse
    {
        return response()->rest(
            FlashSaleResource::collection(
                FlashSale::query()
                    ->active()
                    ->with(['products' => function ($query) {
                        $query
                            ->with(['brand', 'media', 'reviews'])
                            ->where('products.is_visible', true)
                            ->where('products.parent_id', null)
                            ->where('products.stock', '>', 0);
                    }])
                    ->latest('starts_at')
                    ->get()
            )
        );
    }
}
