<?php

namespace Mehrab\OrderService\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mehrab\Common\Responses\AjaxResponses;
use Mehrab\OrderService\Http\Requests\OperatorsRequests\choose_team_leader_store_request;
use Mehrab\OrderService\Http\Requests\OperatorsRequests\failed_order_operator_request;
use Mehrab\OrderService\Models\CancelOrdersService;
use Mehrab\OrderService\Models\OrderService;
use Mehrab\OrderService\Repositories\CancelOrderServiceRepo;
use Mehrab\OrderService\Repositories\OrderServiceRepo;
use Mehrab\RolePermission\Models\Permission;
use Mehrab\Service\Repositories\QuestionRepo;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\User\Repositories\UserRepo;


class OperatorOrderServiceController extends Controller
{
    private $orderServiceRepo;

    public function __construct(OrderServiceRepo $orderServiceRepo)
    {
        $this->orderServiceRepo = $orderServiceRepo;
    }

    public function choose_team_leader(UserRepo $userRepo, OrderServiceRepo $orderServiceRepo, $order_service)
    {
        $order_service = $orderServiceRepo->findByID_check_doesnt_have_team_leader($order_service);
        $this->authorize('choose_team_leader_by_operator', $order_service);

        $team_leaders = $userRepo->get_team_leaders_in_region_and_city($order_service->region->id);
        return view('OrderService::Admin.Operator.list_team_leaders_order_service', compact('team_leaders', 'order_service'));
    }

    public function choose_team_leader_store(choose_team_leader_store_request $request, UserRepo $userRepo, $orderID, $team_leader_ID)
    {
        $order = $this->orderServiceRepo->findByID_check_doesnt_have_team_leader($orderID);
        $this->authorize('choose_team_leader_by_operator', $order);
        if ($this->check_all_canceled_order_service($order)) {
             $userRepo->findByID_available($team_leader_ID);
             $order = $this->orderServiceRepo->choose_team_leader_store($order, $team_leader_ID, $request->message ?? null);
             return updated($order, 'order_service.show', [$orderID], 'done');
        }
        return error();
    }

    public function failed_order_by_operator(failed_order_operator_request $request, OrderServiceRepo $orderServiceRepo, CancelOrderServiceRepo $cancelOrderServiceRepo, $order_service)
    {
        $order = $orderServiceRepo->findByID_check_doesnt_have_team_leader($order_service);
        $this->authorize('operator_cancel_order_service', $order);

        if ($this->check_all_canceled_order_service($order)) {
            $cancel = $cancelOrderServiceRepo->cancel_order_by_operator($request, $order_service);
            $message = 'این سفارش توسط اپراتور ' . auth()->id() . 'مختومه اعلام شد.';
            $order_update = $orderServiceRepo->update_reports($order, OrderService::CANCELED, 'cancel', $message,[auth()->id()]);
            if ($cancel && $order_update) {
                return AjaxResponses::SuccessResponse();
            }
        }
        return AjaxResponses::FailedResponse();

    }

    public function change_team_leader(UserRepo $userRepo, OrderServiceRepo $orderServiceRepo, $order_service)
    {
        $order_service = $orderServiceRepo->findByID_check_order_has_pending_cancel_by_operator($order_service);
        $this->authorize('change_team_leader', $order_service);

        $team_leaders = $userRepo->get_team_leaders_in_region_and_city($order_service->region->id);
        return view('OrderService::Admin.Operator.list_change_team_leaders_order_service', compact('team_leaders', 'order_service'));
    }

    public function change_team_leader_store(choose_team_leader_store_request $request, CancelOrderServiceRepo $cancelOrderServiceRepo, OrderServiceRepo $orderServiceRepo, UserRepo $userRepo, $orderID, $team_leader_ID)
    {
        $order_service = $orderServiceRepo->findByID_check_order_has_pending_cancel_by_operator($orderID);
        $this->authorize('change_team_leader', $order_service);

        $team_leader = $userRepo->findByID_available($team_leader_ID);
        if ($team_leader->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER)) {
            $order = $this->orderServiceRepo->change_team_leader_store($order_service, $team_leader_ID, $request->message ?? null);
            return updated($order, 'order_service.show', [$orderID], 'done');
        }
        return error();
    }

    public function approved_failed_order_by_operator(failed_order_operator_request $request, OrderServiceRepo $orderServiceRepo, CancelOrderServiceRepo $cancelOrderServiceRepo, $order_service)
    {
        $order_service = $orderServiceRepo->findByID_check_order_has_pending_cancel_by_operator($order_service);
        $this->authorize('approved_failed_order_by_operator', $order_service);

        $cancel = $cancelOrderServiceRepo->approve_cancel_order_by_operator($request, $order_service);
        $message = 'درخواست لغو این سفارش توسط اپراتور ' . auth()->id() . 'تایید شد و سفارش مختومه اعلام شد.';
        $order_update = $orderServiceRepo->update_reports($order_service, OrderService::CANCELED, 'cancel', $message,[auth()->id()]);
        if ($cancel && $order_update) {
            return AjaxResponses::SuccessResponse();
        }
        return AjaxResponses::FailedResponse();
    }

    public function check_all_canceled_order_service($order){
        if ($order->status != OrderService::CANCELED && $order->status != OrderService::SUSPENDED)return true;
        return false;
    }
}

