<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use App\Api\Resources\User\UserBaseResource;
use Illuminate\Http\JsonResponse;
use Jason\Api\Api;
use Modules\Mall\Http\Resources\Api\Activity\ActivityBaseResource;
use Modules\Mall\Http\Resources\Api\Activity\ActivityResource;
use Modules\Mall\Http\Resources\Api\Goods\GoodsBaseResource;
use Modules\Mall\Models\Activity;
use Modules\Mall\Models\Goods;

class ActivityController extends Controller
{

    /**
     * Notes: 活动列表
     *
     * @Author: 玄尘
     * @Date: 2022/7/27 11:26
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        $activities = Activity::shown()->get();

        return $this->success(ActivityBaseResource::collection($activities));
    }

    public function show(Activity $activity)
    {
        $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_NORMAL)
            ->get();


        $user = Api::user();

        return $this->success([
            'user'       => $user ? new UserBaseResource($user) : '',
            'show_goods' => $show_goods ? new GoodsBaseResource($show_goods) : '',
            'activity'   => new ActivityResource($activity)
        ]);
    }


}
