<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Services\Admin\OrderService;
use App\libs\Response\GlobalApiResponse;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Models\Order;
use Illuminate\Http\Request;
use Throwable;

class OrdersController extends Controller
{
    private $orderService;

    public function __construct(OrderService $orderService)
    {
        $this->orderService = $orderService;
    }

    public function ordersList(Request $request)
    {
//        $orders = Order::where('type', 'order')->get();
//        foreach ($orders as $order){
//            $order->update(['is_paid' => $order->profile->is_paid]);
//        }
//        return 123;
        $data = $this->orderService->ordersList($request);
        return (new GlobalApiResponse())->success('Orders list', 1, $data);
    }

    public function getOrderDetail($id)
    {
        $data = $this->orderService->getOrderDetail($id);
        return (new GlobalApiResponse())->success('Order Details', 1, $data);
    }

    public function createOrder(Request $request)
    {
        $data = $this->orderService->createOrder($request->all());
        if ($this->orderService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->orderService->getErrors()[0]['message'], $this->orderService->getErrors());
        }
        return (new GlobalApiResponse())->success('Order successfully created');
    }

    public function updateOrder(Request $request, $id)
    {
        $this->orderService->updateOrder($request->all(), $id);
        if ($this->orderService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->orderService->getErrors()[0]['message'], $this->orderService->getErrors());
        }
        return (new GlobalApiResponse())->success('Order successfully updated');
    }

    public function deleteOrder(Request $request)
    {
        if (empty($request->order_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "Order IDs not found!");
        }
        $orders = Order::whereIn('id', $request->order_ids)->get();
        if ($orders) {
            foreach ($orders as $order) {
                try {
                    \DB::beginTransaction();
                    $order->delete();
                    $order->profile->delete();
                    \DB::commit();
                } catch (Throwable $e) {
                    \DB::rollback();
                    return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INTERNAL_SERVER_ERROR, "Oooppp, something went wrong, please try again later.");
                }
            }
            return (new GlobalApiResponse())->success("Order(s) successfully deleted");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Order not found!");
        }
    }

    public function updateOrderStatus(Request $request)
    {
        if (empty($request->order_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "Order IDs not found!");
        }
        $orders = Order::whereIn('id', $request->order_ids)->get();
        if ($orders) {
            foreach ($orders as $order) {
                $order->update(['status' => $request->status]);
            }
            return (new GlobalApiResponse())->success("Order status successfully updated");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Order not found!");
        }
    }
}
