<?php

namespace Modules\User\Services;

use Illuminate\Http\Request;
use Modules\City\Services\CityService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Models\Address;
use Modules\User\Interfaces\AddressRepositoryInterface;

class AddressService
{
    public function __construct(
        private AddressRepositoryInterface $addressRepository,
        private CityService $cityService
    ){}

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

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

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

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

        if (isset($request['not_id']))
        {
            $builder->where('id','!=',$request['not_id']);
        }

        $addresses = $builder->get();
        return $addresses;
    }
    public function checkItem(int $id)
    {
        $address = $this->addressRepository->getItem($id);
        return $address;
    }
    public function getItem(int $id)
    {
        $address = $this->addressRepository->getItem($id);
        return $address;
    }
    public function create(array $request): Address
    {
        $address = $this->addressRepository->create($request);
        return $address;
    }
    public function update(array $request,int $id): Address
    {
        $address = $this->getItem($id);
        if (!$address)
        {
            return abort(404);
        }
        $address = $this->addressRepository->update($request,$address);
        return $address;
    }

    public function createDefault(int $user_id , $user_status)
    {
        $city = $this->cityService->defaultCity();
        $address = $this->create([
            'city_id'=>$city->id,
            'region_id'=>$city->region_id,
            'country_id'=>$city->region->country_id,
            'user_id'=>$user_id,
            'user_status' => $user_status
        ]);
        return $address;
    }

    public function changeCity(array $request)
    {
        $city = $this->cityService->getItem($request['city_id']);
        $this->update([
            'city_id'=>$request['city_id'],
            'region_id'=>$city->region_id,
            'country_id'=>$city->region->country_id
        ],$request['address_id']);
    }

    public function getItemTrashed($id)
    {
        $user = $this->addressRepository->getItemTrashed($id);
        return $user;
    }

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