<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Modules\Cms\Http\Resources\ArticleBaseResource;
use Modules\Cms\Http\Resources\ArticleCollection;
use Modules\Cms\Models\Article;
use Modules\Cms\Models\Category;
use Modules\Mall\Http\Resources\Api\Banner\BannerResource;
use Modules\Mall\Http\Resources\Api\Goods\GoodsBaseResource;
use Modules\Mall\Http\Resources\Api\Video\VideoResource;
use Modules\Mall\Models\Banner;
use Modules\Mall\Models\Goods;
use Modules\Mall\Models\Video;

class IndexController extends Controller
{

    /**
     * Notes   :
     *
     * @Date   : 2021/3/15 5:25 下午
     * @Author : <Jason.C>
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        $banners = Banner::query()->ofPosition(Banner::POSITION_INDEX)->get();

        $show_goods_id = app('Conf_mall')['show_goods_id'] ?? 0;
        $show_goods    = '';
        if ($show_goods_id > 0) {
            $show_goods = Goods::find($show_goods_id);
        }

        $goods = Goods::query()
            ->where('id', '<>', $show_goods_id)
            ->shown()
            ->where('channel', Goods::CHANNEL_SCORE)
            ->get();

        $videos = Video::query()
            ->shown()
            ->get();

        $memories = Article::query()
            ->whereHas('categories', function ($q) {
                $q->where('slug', 'MEMORY');
            })
            ->inRandomOrder()
            ->take(10)
            ->get();

        $memoriesData = [];
        $all          = $memories->count() * 30;
        foreach ($memories as $key => $memory) {
            $id             = ++$key;
            $memoriesData[] = [
                'id'          => $memory->id,
                'title'       => $memory->title,
                'description' => $memory->description,
                'zIndex'      => $id,
                'left'        => (int) bcsub($all, bcmul($key, 30)),
                'image'       => $memory->cover_url,
                'animation'   => null,
            ];
        }

        $healths = Article::query()
            ->whereHas('categories', function ($q) {
                $q->where('slug', 'HEALTH');
            })
            ->inRandomOrder()
            ->take(3)
            ->get();

        $data = [
            'show_goods' => $show_goods ? new GoodsBaseResource($show_goods) : '',
            'videos'     => VideoResource::collection($videos),
            'goods'      => GoodsBaseResource::collection($goods),
            'banners'    => BannerResource::collection($banners),
            'memories'   => $memoriesData,
            'healths'    => ArticleBaseResource::collection($healths),
            'categories' => [
                'memory' => Category::query()->where('slug', 'MEMORY')->value('id') ?? 0,
                'health' => Category::query()->where('slug', 'HEALTH')->value('id') ?? 0,
            ],
        ];

        return $this->success($data);
    }

}
