<?php

namespace Modules\Order\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Order\Enums\OrderStatusEnum;
use Modules\Order\Requests\Web\OrderRequest;
use Modules\Order\Services\CartService;
use Modules\Order\Services\OrderService;
use Modules\Page\Services\PageService;
use Modules\PaymentWay\Services\PaymentWayService;
use Modules\Product\Services\ProductService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Requests\Web\AddressRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\AddressService;
use Modules\User\Services\UserService;

class OrderController extends Controller
{
    private $view = 'web.pages.orders.';

    public function __construct(
        private CartService $cartService,
        private AccountService $accountService,
        private ProductService $productService,
        private OrderService $orderService,
        private AddressService $addressService,
        private UserService $userService,
        private PaymentWayService $paymentWayService,
        private PageService $pageService,
    ){}

    public function checkout(OrderRequest $request)
    {
        $title = __('web.checkout');
        $account = $this->accountService->getAccount();
        $addresses = $this->addressService->get([
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id,
            'not_id'=>$account['user']->address_id,
            'city_id'=>$account['user']->address->city_id
        ]);
        $page = $this->pageService->getItemById(2);
        $address = $this->addressService->getItem($account['user']->address_id);
        $products = $this->cartService->getByBrand([
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id,
        ]);
        $payment_ways = $this->paymentWayService->get();
        return view($this->view.'checkout',get_defined_vars());
    }

    public function orderCheckout(OrderRequest $request)
    {
        $url = $this->orderService->create($request->all());
        $data = [
            'status'=>200,
            'data'=>$url,
            'message'=>__('web.success')
        ];
        return ResponseService::response($data);
    }

    public function addressChange(Request $request)
    {
        $account = $this->accountService->getAccount();
        $this->userService->update([
            'address_id'=>$request->address_id,
        ],$account['user']->id);
        session()->flash(__('web.success'));
        return redirect()->back();
    }

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

    public function orderDetails($id)
    {
        $title = __('web.orderDetails');
        $order = $this->orderService->getItem($id);
        if (!$order)
        {
            return abort(404);
        }
        return view($this->view.'details',get_defined_vars());

    }
    public function track($id)
    {
        $title = __('web.orderTrack');
        $order = $this->orderService->getItem($id);
        if (!$order)
        {
            return abort(404);
        }
        $statuses = OrderStatusEnum::values_without_canceled();
        $tracks = $this->orderService->tracks($order->id);
        $stacks_status = $tracks->pluck('status')->toArray();
        $statuses = array_diff($statuses,$stacks_status);
        return view($this->view.'track',get_defined_vars());

    }

    public function fetch(Request $request)
    {
        $account = $this->accountService->getAccount();
        $rejected = OrderStatusEnum::CANCELED->value;
        $orders = $this->orderService->get([
            'notTypeStatus' => OrderStatusEnum::CANCELED->value,
            'search' => $request->search,
            'showLatest' => $request->showLatest,
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id
        ]);
        $canceled_orders = $this->orderService->get([
            'typeStatus' => OrderStatusEnum::CANCELED->value,
            'search' => $request->search,
            'showLatest' => $request->showLatest,
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id
        ]);
        $showOrder = $request->showOrder;
        return view($this->view.'orders',get_defined_vars());
    }
}
