<?php

namespace Modules\Address\Services;


use Modules\Address\Interfaces\AddressRepositoryInterface;
use Modules\Address\Models\Address;

class AddressService
{
    private AddressRepositoryInterface $addressRepositoryInterface;

    public function __construct(AddressRepositoryInterface $addressRepositoryInterface)
    {
        $this->addressRepositoryInterface = $addressRepositoryInterface;
    }

    public function get(array $request)
    {
        $builder = $this->addressRepositoryInterface->get($request);

        if (@$request['id']) {
            $builder->where('addresses.id', $request['id']);
        }
        if (@$request['search']) {
            $builder->where('addresses.description', 'LIKE', '%' . $request['search'] . '%');
        }
        if (@$request['city_id']) {
            $builder->where('city_id', $request['city_id']);
        }
        if (@$request['user_id']) {
            $builder->where('addresses.user_id', $request['user_id']);
        }

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

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

        if (@$request['paginate']) {
            $addresses = $builder->paginate(50);
        } elseif (@$request['api']) {
            $addresses = $builder;
        } else {
            $addresses = $builder->cursor();
        }
        return $addresses;
    }
    public function checkItem($id)
    {
        $address = $this->addressRepositoryInterface->checkItem($id);
        return $address;
    }

    public function getItem($id)
    {
        $address = $this->checkItem($id);
        return $address;
    }

    public function getItemTrashed($id)
    {
        $address = $this->addressRepositoryInterface->getItemTrashed($id);
        return $address;
    }



    public function create(array $request): Address
    {
        $address = $this->addressRepositoryInterface->create($request);
        return $address;
    }
    public function update(array $request, int $id): Address
    {

        $address = $this->checkItem($id);
        $address = $this->addressRepositoryInterface->update($request, $address);
        return $address;
    }
    public function destroy(int $id): void
    {
        $address = $this->checkItem($id);
        if ($address) {
            $this->addressRepositoryInterface->destroy($address);
        } else {
            $address = $this->getItemTrashed($id);
            $this->addressRepositoryInterface->restore($address);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->addressRepositoryInterface->destroyAll($request['ids']);
        } else {
            $this->addressRepositoryInterface->restoreAll($request['ids']);
        }
    }
}
