<?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\PaymentCardResource;
use Modules\User\Requests\Api\Address\AddressRequest;
use Modules\User\Requests\Api\PaymentCard\PaymentCardRequest;
use Modules\User\Requests\Api\PaymentCard\ShowRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;

class PaymentCardController extends Controller
{

    public function __construct(
        private AccountService $accountService,
        private UserService $userService,
    ){}

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();

        $cards = $this->userService->cards($account['user']->id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>PaymentCardResource::collection($cards)
        ]);
    }

    public function create(PaymentCardRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $data = $request->validated();
        $data['user_id'] = $user->id;

        $this->userService->createCard($data);

        $cards = $this->userService->cards($user->id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => PaymentCardResource::collection($cards)
        ];
        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
    {
        $card = $this->userService->getCard($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new PaymentCardResource($card)
        ]);
    }

    public function edit(PaymentCardRequest $request,int $id)
    {
        $account = $this->accountService->getAccount();
        $this->userService->updateCard($request->validated(),$id);

        $cards = $this->userService->cards($account['user']->id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => PaymentCardResource::collection($cards)
        ];
        return ResponseService::response($response);
    }

    public function delete(ShowRequest $request,int $id) : JsonResponse
    {
        $account = $this->accountService->getAccount();
        $this->userService->deleteCard($id);

        $cards = $this->userService->cards($account['user']->id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => PaymentCardResource::collection($cards)
        ];
        return ResponseService::response($response);
    }

}
