<?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 AllOrderServiceManagerController extends Controller
{
    private $orderServiceRepo;

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

    public function failed_order_by_all_order_service_manager(failed_order_operator_request $request, OrderServiceRepo $orderServiceRepo, CancelOrderServiceRepo $cancelOrderServiceRepo, $order_service)
    {
        $order = $orderServiceRepo->findByID_check_all_order_service_manager_can_cancel($order_service);
        $this->authorize('all_order_service_manager_cancel', OrderService::class);

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

    }

    public function change_team_leader_by_all_order_service_manager(UserRepo $userRepo, OrderServiceRepo $orderServiceRepo, $order_service)
    {
        $order_service = $orderServiceRepo->findByID_check_all_order_service_manager_can_change_team_leader($order_service);
        $this->authorize('change_team_leader_by_all_order_service_manager', OrderService::class);

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

    public function change_team_leader_by_all_order_service_manager_store(choose_team_leader_store_request $request, OrderServiceRepo $orderServiceRepo, UserRepo $userRepo, $orderID, $team_leader_ID)
    {
        $order_service = $orderServiceRepo->findByID_check_all_order_service_manager_can_change_team_leader($orderID);
        $this->authorize('change_team_leader_by_all_order_service_manager', OrderService::class);

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