<?php

namespace Modules\News\Services;

use Modules\News\Interfaces\NewsRepositoryInterface;
use Modules\News\Models\News;

class NewsService
{
    public function __construct(private NewsRepositoryInterface $newsRepository)
    {
    }

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

        if (!empty($request['title'])) {
            $builder->where('nd.title', 'LIKE', '%' . $request['title'] . '%');
        }
        if (!empty($request['date_from'])) {
            $builder->whereDate('news.created_at', '>=', $request['date_from']);
        }
        if (!empty($request['date_to'])) {
            $builder->whereDate('news.created_at', '<=', $request['date_to']);
        }
        if (isset($request['status']) && (int) $request['status'] === 0) {
            $builder->onlyTrashed();
        }

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

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

    public function checkItem(int $id)
    {
        return $this->newsRepository->checkItem($id);
    }

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

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

        return $news;
    }

    public function getItemTrashed(int $id)
    {
        return $this->newsRepository->getItemTrashed($id);
    }

    public function create(array $request): News
    {
        $news = $this->newsRepository->create($request);
        $this->newsRepository->saveData($request, $news->id);

        return $news;
    }

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

        $news = $this->newsRepository->update($request, $news);
        $this->newsRepository->saveData($request, $news->id);

        return $news;
    }

    public function destroy(int $id): void
    {
        $news = $this->checkItem($id);
        if ($news) {
            $this->newsRepository->destroy($news);
        }
    }

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

    public function forceDelete(int $id): void
    {
        $trashed = $this->getItemTrashed($id);
        if ($trashed) {
            $this->newsRepository->forceDelete($trashed);
        }
    }

    public function forceDeleteAll(array $ids): void
    {
        $this->newsRepository->forceDeleteAll($ids);
    }

    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->newsRepository->destroyAll($request['ids']);
        } else {
            $this->newsRepository->restoreAll($request['ids']);
        }

    }
}
