<?php

namespace Modules\Banner\Services;

use App\Services\Upload\UploadService;
use Modules\Banner\Interfaces\BannerRepositoryInterface;
use Modules\Banner\Models\Banner;

class BannerService
{
    public function __construct(private BannerRepositoryInterface $bannerRepository)
    {
    }

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

        if (!empty($request['title'])) {
            $builder->where('bd.title', 'LIKE', '%' . $request['title'] . '%');
        }

        if (!empty($request['date_from'])) {
            $builder->whereDate('banners.created_at', '>=', $request['date_from']);
        }

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

        // Handle trashed items
        if (isset($request['status']) && (int) $request['status'] === 0) {
            $builder->onlyTrashed();
        }

        if (!empty($request['orderBy'])) {
            $builder->orderBy($request['orderBy']['key'], $request['orderBy']['value']);
        } else {
            $builder->orderBy('banners.id', 'DESC');
        }

        return !empty($request['paginate']) ? $builder->paginate(50) : $builder->cursor();
    }

    public function checkItem(int $id): ?Banner
    {
        return $this->bannerRepository->checkItem($id);
    }

    public function getItem(int $id)
    {
        $banner = $this->checkItem($id);
        if (! $banner) {
            return null;
        }

        $descriptions = $this->bannerRepository->getItem($id);
        foreach ($descriptions as $row) {
            $banner[$row->local] = $row;
        }

        return $banner;
    }

    public function getItemTrashed(int $id): ?Banner
    {
        return $this->bannerRepository->getItemTrashed($id);
    }

    public function create(array $data): Banner
    {
        $banner = $this->bannerRepository->create($data);
        $this->bannerRepository->saveDescriptions($banner->id, $data);

        return $banner;
    }

    public function update(int $id, array $data): ?Banner
    {
        $banner = $this->checkItem($id);
        if (! $banner) {
            return null;
        }

        $banner = $this->bannerRepository->update($banner, $data);
        $this->bannerRepository->saveDescriptions($banner->id, $data);

        return $banner;
    }

    public function destroy(int $id): bool
    {
        $banner = $this->checkItem($id);
        if (! $banner) {
            return false;
        }

        $this->bannerRepository->destroy($banner);
        return true;
    }

    public function destroyAll(array $ids): bool
    {
        $this->bannerRepository->destroyAll($ids);
        return true;
    }

    public function forceDelete(int $id): bool
    {
        $banner = $this->getItemTrashed($id);
        if (! $banner) {
            return false;
        }

        UploadService::delete($banner->image);
        $this->bannerRepository->forceDelete($banner);
        return true;
    }

    public function forceDeleteAll(array $ids): bool
    {
        $banners = Banner::onlyTrashed()->whereIn('id', $ids)->get();
        foreach ($banners as $banner) {
            UploadService::delete($banner->image);
        }

        $this->bannerRepository->forceDeleteAll($ids);
        return true;
    }

    public function restore(int $id): void
    {
        $banner = $this->getItemTrashed($id);
        if ($banner) {
            $this->bannerRepository->restore($banner);
        }
    }

    public function restoreAll(array $ids): void
    {
        $this->bannerRepository->restoreAll($ids);
    }
}
