<?php

namespace Modules\Address\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Address\Models\Address;
use Modules\Address\Requests\Api\AddressRequest;
use Modules\Address\Resources\Api\AddressResource;
use Modules\Address\Services\AddressService;


class AddressController extends Controller
{

    public function __construct(
        private AddressService $addressService,
    ) {
    }

    /**
     * Get Addresses.
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): void
    {
        $credentials['user_id'] = auth()->id();
        $credentials['api'] = true;
        $query = $this->addressService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => AddressResource::class,
            "returnName" => "addresses"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Get Address.
     *
     * @param Request $request
     * @param Address $address
     * @return JsonResponse
     */
    public function show(Request $request, Address $address): JsonResponse
    {
        $category = $this->addressService->getItem($address->id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new AddressResource($category)
        ];
        return ResponseService::response($response);
    }
    /**
     * Create Address.
     *
     * @param AddressRequest $request
     * @return void
     */
    public function store(AddressRequest $request): void
    {
        $credentials = $request->only('latitude', 'longitude', 'city_id', 'description');
        $credentials['user_id'] = auth()->id();
        $this->addressService->create($credentials);

        $query = $this->addressService->get(['api' => true, 'user_id' => $credentials['user_id']]);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => AddressResource::class,
            "returnName" => "addresses"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Edit Address.
     *
     * @param AddressRequest $request
     * @param Address $address
     * @return void
     */
    public function update(AddressRequest $request, Address $address): void
    {
        $credentials = $request->only('latitude', 'longitude', 'city_id', 'description');


        $this->addressService->update($credentials, $address->id);

        $query = $this->addressService->get(['api' => true, 'user_id' => auth()->id()]);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => AddressResource::class,
            "returnName" => "addresses"
        ];
        ResponseService::fetchPainateDate($response);
    }

    /**
     * Remove Address.
     *
     * @param Request $request
     * @param Address $address
     * @return void
     */
    public function delete(Request $request, Address $address): void
    {
        if ($address->user_id !== auth()->id()) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $this->addressService->destroy($address->id);

        $credentials['api'] = true;
        $credentials['user_id'] = auth()->id();
        $query = $this->addressService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => AddressResource::class,
            "returnName" => "addresses"
        ];
        ResponseService::fetchPainateDate($response);
    }
}
