<?php

namespace Modules\User\Controllers\Api;

/** Ajax Request */

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

class AddressController extends Controller
{

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

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $addresses = $this->addressService->get($request);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>AddressResource::collection($addresses)
        ]);
    }

    public function create(AddressRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $data = $request->validated();
        $data['user_status'] = $account['status'];
        $data['user_id'] = $user->id;
        $data['warehouse_id'] = $user->address->city->warehouses->first()->warehouse_id;

        $this->addressService->create($data);
        $request =
            [
                'user_id'=>$account['user']->id,
                'status'=>$account['status']
            ];
        $addresses = $this->addressService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => AddressResource::collection($addresses)
        ];
        return ResponseService::response($response);
    }
    /**
     * Show the form for editing the specified Country.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function show(ShowRequest $request,int $id) : JsonResponse
    {
        $address = $this->addressService->getItem($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new AddressResource($address)
        ]);
    }

    public function edit(AddressRequest $request,int $id)
    {
        $account = $this->accountService->getAccount();
        $this->addressService->update($request->validated(),$id);
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $addresses = $this->addressService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => AddressResource::collection($addresses)
        ];
        return ResponseService::response($response);
    }

    public function delete(ShowRequest $request,int $id) : JsonResponse
    {
        $account = $this->accountService->getAccount();
        $this->addressService->destroy($id);
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $addresses = $this->addressService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => AddressResource::collection($addresses)
        ];
        return ResponseService::response($response);
    }

}
