<?php

namespace Modules\Refund\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Order\Services\OrderService;
use Modules\Refund\Requests\Api\CreateRequest;
use Modules\Refund\Requests\Api\ShowRequest;
use Modules\Refund\Models\Resources\RefundResource;
use Modules\Refund\Services\RefundService;
use Modules\User\Services\AccountService;

class RefundController extends Controller
{

    public function __construct(
        private AccountService $accountService,
        private RefundService $refundService,
        private OrderService $orderService,
    ){}

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

        $refunds = $this->refundService->get([
            'user_id'=>$account['user']->id
        ]);

        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>RefundResource::collection($refunds),
        ]);
    }

    public function create(CreateRequest $request)
    {
        $account = $this->accountService->getAccount();
        $data = $request->validated();
        $product = $this->orderService->getProduct([
            'id'=>$request->order_product_id,
            'user_id'=>auth()->user()->id
        ]);
        $data['address_id'] = auth()->user()->address_id;
        $data['order_product_id'] = $request->order_product_id;
        $data['order_id'] = $product->order_id;
        $this->refundService->create($data);

        $refunds = $this->refundService->get([
            'user_id'=>$account['user']->id
        ]);

        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => RefundResource::collection($refunds)
        ];
        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
    {
        $cart = $this->refundService->getItem($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new RefundResource($cart)
        ]);
    }


}
