<?php

namespace Modules\Review\Services;

use Modules\Review\Interfaces\ReviewRepositoryInterface;
use Modules\Review\Models\Review;

class ReviewService
{
    public function __construct(private ReviewRepositoryInterface $reviewRepository)
    {
    }

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

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

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

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

        if (!empty($request['date_to'])) {
            $builder->whereDate('reviews.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('reviews.id', 'DESC');
        }

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

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

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

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

        return $review;
    }

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

    public function create(array $request): Review
    {
        $review = $this->reviewRepository->create($request);
        $this->reviewRepository->saveData($request, $review->id);
        return $review;
    }

    public function update(array $request, int $id): Review
    {
        $review = $this->checkItem($id);
        if (! $review) {
            abort(404);
        }

        $review = $this->reviewRepository->update($request, $review);
        $this->reviewRepository->saveData($request, $review->id);
        return $review;
    }

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

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

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

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

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