<?php

namespace Modules\Refund\Controllers\Web;

/** Ajax Request */

use App\Services\Response\ResponseService;
use App\Services\Upload\FileService;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Order\Services\OrderService;
use Modules\Page\Services\PageService;
use Modules\Reason\Services\ReasonService;
use Modules\Refund\Enums\RefundStatusEnum;
use Modules\Refund\Requests\Web\Refund1Request;
use Modules\Refund\Requests\Web\Refund2Request;
use Modules\Refund\Requests\Web\Refund3Request;
use Modules\Refund\Requests\Web\Refund4Request;
use Modules\Refund\Requests\Web\Refund5Request;
use Modules\Refund\Services\RefundService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Services\AddressService;
use Modules\User\Services\UserService;

class RefundController extends Controller
{

    /** View folder */
    private $viewDirectory = 'web.pages.refunds.';

    //

    public function __construct
    (
        private AddressService $addressService,
        private RefundService $refundService,
        private ReasonService $reasonService,
        private OrderService $orderService,
        private PageService $pageService,
        private UserService $userService,
    ){}

    public function index()
    {
        $title = __('web.refund');
        $user = auth()->user();
        $refunds = $this->refundService->get([
            'user_id'=>$user->id
        ]);
        return view( 'web.pages.profile.refund', get_defined_vars());
    }

    public function show($id)
    {
        $title = __('web.refund_show');
        $refund = $this->refundService->getItem($id);
        $product = $refund->orderProduct;
        $statuses = RefundStatusEnum::mainCases();
        $user = auth()->user();
        $policy = $this->pageService->getItemById(6);
        return view($this->viewDirectory . 'show', get_defined_vars());
    }
    /**
     * Display a listing of the refunds.
     *
     * @param Request $request
     * @return View
     */
    public function step1($id): View
    {
        $title = __('web.refund_step1');
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        if(!$product)
        {
            return abort(404);
        }
        $policy = $this->pageService->getItemById(6);
        $reasons = $this->reasonService->get();
        $action = route('web.refunding.step1',$id);
        return view($this->viewDirectory . 'step1', get_defined_vars());
    }
    public function refund1(Refund1Request $request,$id)
    {
        Session::put('reason_id',$request->reason_id);
        return ResponseService::response([
            'message'=>__('web.success'),
            'data'=>route('web.refund.step2',$id),
            'status'=>200
        ]);
    }
    public function step2($id): View
    {
        $title = __('web.refund_step2');
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        $reason_id = Session::get('reason_id');
        if(!$product || !$reason_id)
        {
            return abort(404);
        }
        $policy = $this->pageService->getItemById(6);
        $action = route('web.refunding.step2',$id);
        $reason = $this->reasonService->getItem($reason_id);
        return view($this->viewDirectory . 'step2', get_defined_vars());
    }
    public function refund2(Refund2Request $request,$id)
    {
        Session::put([
            'note'=>$request->note,
            'image'=>$request->image ? FileService::uploadImage($request->image) : null,
        ]);
        return ResponseService::response([
            'message'=>__('web.success'),
            'data'=>route('web.refund.step3',$id),
            'status'=>200
        ]);
    }
    public function step3($id): View
    {
        $title = __('web.refund_step3');
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        $reason_id = Session::get('reason_id');
        $note = Session::get('note');
        $image = asset(Session::get('image'));
        if(!$product || !$reason_id)
        {
            return abort(404);
        }
        $policy = $this->pageService->getItemById(6);
        $action = route('web.refunding.step3',$id);
        $reason = $this->reasonService->getItem($reason_id);
        $cards = $this->userService->cards(auth()->user()->id);
        return view($this->viewDirectory . 'step3', get_defined_vars());
    }
    public function refund3(Refund3Request $request,$id)
    {
        Session::put([
            'user_card_id'=>$request->user_card_id
        ]);
        return ResponseService::response([
            'message'=>__('web.success'),
            'data'=>route('web.refund.step4',$id),
            'status'=>200
        ]);
    }
    public function step4($id): View
    {
        $title = __('web.refund_step4');
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        $reason_id = Session::get('reason_id');
        $note = Session::get('note');
        $image = asset(Session::get('image'));
        $user_card_id = Session::get('user_card_id');
        if(!$product || !$reason_id || !$user_card_id)
        {
            return abort(404);
        }
        $policy = $this->pageService->getItemById(6);
        $action = route('web.refunding.step4',$id);
        $reason = $this->reasonService->getItem($reason_id);
        $user = auth()->user();
        $address = $this->addressService->getItem($user->address_id);
        $card = $this->userService->getCard($user_card_id);
        return view($this->viewDirectory . 'step4', get_defined_vars());
    }
    public function refund4(Refund4Request $request,$id)
    {
        $user = auth()->user();
        Session::put([
            'address_id'=>$user->address_id
        ]);
        return ResponseService::response([
            'message'=>__('web.success'),
            'data'=>route('web.refund.step5',$id),
            'status'=>200
        ]);
    }
    public function step5($id): View
    {
        $title = __('web.refund_step5');
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        $reason_id = Session::get('reason_id');
        $note = Session::get('note');
        $image = asset(Session::get('image'));
        $user_card_id = Session::get('user_card_id');
        if(!$product || !$reason_id || !$user_card_id)
        {
            return abort(404);
        }
        $policy = $this->pageService->getItemById(6);
        $action = route('web.refunding.step5',$id);
        $user = auth()->user();
        $address = $this->addressService->getItem($user->address_id);
        $reason = $this->reasonService->getItem($reason_id);
        $card = $this->userService->getCard($user_card_id);
        return view($this->viewDirectory . 'step5', get_defined_vars());
    }
    public function refund5(Refund5Request $request,$id)
    {
        $data = Session::all();
        $data['order_product_id'] = $id;
        $product = $this->orderService->getProduct([
            'id'=>$id,
            'user_id'=>auth()->user()->id
        ]);
        $data['order_id'] = $product->order_id;
        $refund = $this->refundService->create($data);
        Session::forget([
            'reason_id','note','image','user_card_id','address_id'
        ]);
        return ResponseService::response([
            'message'=>__('web.success'),
            'data'=>route('web.refund.show',$refund->id),
            'status'=>200
        ]);
    }
}
