<?php

namespace Modules\Banner\Services;

use Modules\Banner\Enums\BannerActiveStatusEnum;
use Modules\Banner\Enums\BannerCountPhotoEnum;
use Modules\Banner\Enums\BannerStatusEnum;
use Modules\Banner\Interfaces\BannerRepositoryInterface;
use Modules\Banner\Models\Banner;

class BannerService
{
    private BannerRepositoryInterface $bannerRepository ;

    public function __construct(BannerRepositoryInterface $bannerRepository)
    {
        $this->bannerRepository = $bannerRepository;
    }

    public function get(array $request = [])
    {
        $builder = $this->bannerRepository->get($request);

        if (isset($request['typeStatus']))
        {
            $builder->where('status',$request['typeStatus']);
        }

        if (isset($request['typeStatuses']))
        {
            $builder->whereIn('status',$request['typeStatuses']);
        }

        if (@$request['type_id'])
        {
            $builder->where('type_id',$request['type_id']);
        }

        if (@$request['category_id'])
        {
            $builder->where('category_id',$request['category_id']);
        }

        if (@$request['date_from'])
        {
            $builder->whereDate('created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('created_at', '<=', $request['date_to']);
        }

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['orderBy'])
        {
            $builder->orderBy($request['orderBy']['key'],$request['orderBy']['value']);
        }
        else
        {
            $builder->orderBy('id','ASC');
        }

        if (@$request['paginate']) {
            $banners= $builder->paginate(50);
        }else{
            $banners=$builder->get();
        }
        return $banners;
    }
    public function checkItem($id)
    {
        $banner = $this->bannerRepository->checkItem($id);
        return $banner;
    }

    public function getItem($id)
    {
        $banner = $this->checkItem($id);

        return $banner;
    }

    public function photos($id)
    {
        return $this->bannerRepository->photos($id);
    }

    public function getItemTrashed($id)
    {
        $banner = $this->bannerRepository->getItemTrashed($id);
        return $banner;
    }

    public function create(array $request): Banner
    {
        $banner = $this->bannerRepository->create($request);
        return $banner;
    }
    public function update(array $request,int $id): Banner
    {
        $banner = $this->checkItem($id);
        $banner = $this->bannerRepository->update($request,$banner);
        $this->createPhotos($request,$id);
        return $banner;
    }

    public function createPhotos(array $request,int $id):void
    {
        $this->bannerRepository->deletePhotos($id);
        if(isset($request['images']))
        {
            foreach ($request['images'] as $row)
            {
                $data = [
                    'image'=>$row['image'],
                    'link'=>$row['link'] ?? null,
                    'banner_id'=>$id
                ];
                $this->bannerRepository->createPhotos($data);
            }
        }
    }

    public function createBannerCategory($category_id)
    {
        $content =
        [
            'category_id'=>$category_id,
            'status'=>BannerStatusEnum::CATEGORY->value,
            'active_status'=>BannerActiveStatusEnum::INACTIVE->value,
            'count_photo'=>BannerCountPhotoEnum::ONE->value
        ];
        $this->create($content);
    }

}
