<?php

namespace Modules\Banner\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Banner\Enums\BannerStatusEnum;
use Modules\Banner\Models\Resources\BannerResource;
use Modules\Banner\Services\BannerService;
use Modules\User\Services\AccountService;

class BannerController extends Controller
{

    public function __construct(
        private AccountService $accountService,
        private BannerService $bannerService,
    ){}

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function home(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];

        $banners = $this->bannerService->get([
            'type_id' => $user->type_id,
            'category_id' => null,
            'typeStatus' => BannerStatusEnum::MAIN_PAGE->value
        ]);

        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>BannerResource::collection($banners)
        ]);
    }

    public function footer(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $banner = $this->bannerService->get([
            'type_id' => $user->type_id,
            'category_id' => null,
            'typeStatus' => BannerStatusEnum::FOOTER->value
        ])->first();
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new BannerResource($banner)
        ]);
    }

    /**
     * Show the form for editing the specified Banner.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function show(int $id) : JsonResponse
    {
        $banner = $this->bannerService->checkItem($id);
        if (!$banner)
        {
            abort(404);
        }
        $banner = $this->bannerService->getItem($id);

        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new BannerResource($banner)
        ]);
    }


}
