<?php

namespace App\Http\Controllers\Web;

use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Modules\City\Services\CityService;
use Modules\Order\Models\Order;
use Modules\Order\Requests\Web\CheckoutAddressRequest;
use Modules\Order\Requests\Web\CheckoutWayRequest;
use Modules\Order\Requests\Web\OrderRequest;
use Modules\Order\Services\CartService;
use Modules\Order\Services\OrderService;
use Modules\Rate\Requests\Web\RateRequest;
use Modules\Rate\Services\RateService;
use Modules\Shipping\Models\Shipping;
use Modules\Shipping\Models\ShippingCity;
use Modules\Shipping\Services\ShippingService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Models\Address;
use Modules\User\Services\AccountService;
use Modules\User\Services\AddressService;
use Modules\Way\Models\Way;
use Modules\Way\Services\WayService;


class OrderController extends Controller
{
    private $view = 'web.pages.orders.';
    private AccountService $accountService ;
    private AddressService $addressService ;
    private CityService $cityService ;
    private WayService $wayService ;
    private ShippingService $shippingService ;
    private CartService $cartService ;
    private OrderService $orderService ;
    private RateService $rateService ;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(
        AccountService $accountService,
        AddressService $addressService,
        CityService $cityService,
        WayService $wayService,
        ShippingService $shippingService,
        CartService $cartService,
        OrderService $orderService,
        RateService $rateService,
    )
    {
        $this->accountService = $accountService;
        $this->addressService = $addressService;
        $this->cityService = $cityService;
        $this->wayService = $wayService;
        $this->shippingService = $shippingService;
        $this->cartService = $cartService;
        $this->orderService = $orderService;
        $this->rateService = $rateService;
    }

    public function address(Request $request)
    {
        $cities = $this->cityService->get($request);
        $account = $this->accountService->getAccount();
        $request->merge(['status'=>$account['status'],'user_id'=>$account['user']->id]);
        $count = $this->cartService->get($request)->count();
        if ($count == 0)
        {
            Session::flash('error',__('web.not_found_product_cart'));
            return redirect(route('cart.index'));
        }
        $title = __('web.order_address');
        $addresses = $this->addressService->get($request);
        $action = route('web.checkout.addressing');
        $areas =[];
        return view($this->view.'checkout_address',get_defined_vars());
    }

    public function addressCheckout(CheckoutAddressRequest $request) :JsonResponse
    {
        $account = $this->accountService->getAccount();
        if ($request->locationType != 'newLocation')
        {
            Session::put(['address_id'=>$request->address_id]);
        }
        else
        {
            $request->merge(['user_status'=>$account['status'],'user_id'=>$account['user']->id]);
            $address = $this->addressService->save($request,null);
            Session::put(['address_id'=>$address->id]);
        }
        if ($account['status'] == AccountStatusEnum::VISITOR->value)
        {
            $this->accountService->update($request);
        }


        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('web.checkout.way'),
        ];
        return ResponseService::response($response);
    }

    public function way(Request $request)
    {
        $title = __('web.order_way');
        $address_id = Session::get('address_id');
        if (!$address_id)
        {
            Session::flash('error',__('web.not_found_address'));
            return  redirect()->back();
        }
        $ways = $this->wayService->get($request);
        $address = Address::findOrFail($address_id);
        $address = $this->addressService->getItem($address);
        $shippings = $this->shippingService->get($request);
        $action = route('web.checkout.waying');
        return view($this->view.'checkout_way',get_defined_vars());
    }

    public function wayCheckout(CheckoutWayRequest $request) :JsonResponse
    {

        Session::put([
            'way_id'=>$request->way_id,
            'shipping_id'=>$request->shipping_id,
        ]);
//        $address = Address::find(Session::get('address_id'));
//        $shipping_city = ShippingCity::where('shipping_id',$request->shipping_id)->where('city_id',$address->city_id)->first();
//        Session::put(['shipping_city_id'=>$shipping_city->id]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('web.checkout.order'),
        ];
        return ResponseService::response($response);
    }

    public function order()
    {
        $title = __('web.order_checkout');
        $address_id = Session::get('address_id');
        $way_id = Session::get('way_id');
        $shipping_id = Session::get('shipping_id');
        if (!$address_id || !$way_id || !$shipping_id)
        {
            return  redirect()->back();
        }
        $date = date('d');
        $date .= ' '.__('web.monthes.'.date('m'));
        $date .= ' '.date('Y');
        $address = Address::findOrFail($address_id);
        $address = $this->addressService->getItem($address);
        $way = Way::findOrFail($way_id);
        $way = $this->wayService->getItem($way);
        $shipping = Shipping::findOrFail($shipping_id);
        $shipping = $this->shippingService->getItem($shipping);
        $action = route('web.checkout.ordering');
        return view($this->view.'checkout_order',get_defined_vars());
    }

    public function orderCheckout(OrderRequest $request)
    {
        $url = $this->orderService->create($request);

        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> $url,
        ];
        return ResponseService::response($response);
    }

    public function payment(Request $request)
    {
        $user = $this->accountService->getAccount();
        $status = $user['status'];
        $order = $this->orderService->paymentStatus($request);
        if ($order)
        {
            session()->flash('success', __('web.success'));
        }
        else
        {
            session()->flash('error', __('web.failed'));
        }
        $url = $status == AccountStatusEnum::USER->value ? route('web.profile') : route('home');
        return redirect($url);
    }

    public function orders(Request $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_status'=>$account['status'],'user_id'=>$account['user']->id]);
        $orders = $this->orderService->get($request);
        return view($this->view.'orders',get_defined_vars());
    }

    public function orderDetails($id)
    {
        $title = __('web.order_details');
        $account = $this->accountService->getAccount();
        $order = Order::where('id',$id)->where('user_id',$account['user']->id)->where('user_status',$account['status'])->firstOrFail();

        $order = $this->orderService->getItem($order);
        return view($this->view.'details',get_defined_vars());
    }
    public function bill($id)
    {
        $title = __('web.bill');
        $account = $this->accountService->getAccount();
        $order = Order::where('id',$id)->where('user_id',$account['user']->id)->where('user_status',$account['status'])->firstOrFail();

        $order = $this->orderService->getItem($order);
        return view($this->view.'bill',get_defined_vars());
    }
    public function rate(RateRequest $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['status'=>$account['status'],'user_id'=>$account['user']->id]);
        $this->rateService->save($request,null);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> url()->previous(),
        ];
        return ResponseService::response($response);
    }
}
