<?php

namespace Mehrab\OrderService\Http\Controllers;

use App\Http\Controllers\Controller;
use Mehrab\Common\Responses\AjaxResponses;
use Mehrab\OrderService\Http\Requests\TeamLeadersRequests\change_expert_store_request;
use Mehrab\OrderService\Http\Requests\TeamLeadersRequests\choose_expert_store_request;
use Mehrab\OrderService\Http\Requests\TeamLeadersRequests\failed_order_team_leader_request;
use Mehrab\OrderService\Models\OrderService;
use Mehrab\OrderService\Repositories\CancelOrderServiceRepo;
use Mehrab\OrderService\Repositories\OrderServiceRepo;
use Mehrab\RolePermission\Models\Permission;
use Mehrab\User\Repositories\UserRepo;


class TeamLeaderOrderServiceController extends Controller
{
    private $orderServiceRepo;

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

    public function choose_expert(UserRepo $userRepo, OrderServiceRepo $orderServiceRepo, $order_service)
    {
        $order_service =$orderServiceRepo->findByID_check_doesnt_have_expert($order_service);
        $this->authorize('choose_expert_by_team_leader', $order_service);

        $experts = $userRepo->get_experts_related_team_leader($order_service->team_leader);
        return view('OrderService::Admin.TeamLeader.list_experts_order_service', compact('experts', 'order_service'));
    }

    public function choose_expert_store(choose_expert_store_request $request, CancelOrderServiceRepo $cancelOrderServiceRepo, OrderServiceRepo $orderServiceRepo, UserRepo $userRepo, $orderID, $expertID){
        $order_service =$orderServiceRepo->findByID_check_doesnt_have_expert($orderID);
        $this->authorize('choose_expert_by_team_leader', $order_service);

        if ($cancelOrderServiceRepo->check_all_canceled_order_service($orderID)) {
            $expert = $userRepo->findByID_where_related_team_leader($expertID);
            if ($expert->hasPermissionTo(Permission::PERMISSION_EXPERT)) {
                $order = $this->orderServiceRepo->choose_expert_store($order_service, $expertID, $request->message ?? null);
                return updated($order, 'order_service.show', [$orderID], 'done');
            }
        }
        return error();
    }

    public function failed_order_by_team_leader(failed_order_team_leader_request $request, OrderServiceRepo $orderServiceRepo, CancelOrderServiceRepo $cancelOrderServiceRepo, $order_service)
    {
        $order = $orderServiceRepo->findByID_check_doesnt_have_expert($order_service);
        $this->authorize('team_leader_cancel_order_service', $order);

        if ($cancelOrderServiceRepo->check_all_canceled_order_service($order_service) ) {
            $cancel = $cancelOrderServiceRepo->cancel_order_by_team_leader($request, $order_service);
            $message = 'درخواست لغو توسط سرتیم ' . auth()->id() . 'ارسال شد.';
            $order_update = $orderServiceRepo->update_reports($order,OrderService::SUSPENDED, 'suspended',$message,[auth()->id(),$order->operator_id]);
            if ($cancel && $order_update) {
                return  AjaxResponses::SuccessResponse();
            }
        }
        return AjaxResponses::FailedResponse();
    }

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

        $experts = $userRepo->get_experts_related_team_leader($order_service->team_leader);
        return view('OrderService::Admin.TeamLeader.list_change_experts_order_service', compact('experts', 'order_service'));
    }

    public function change_expert_store(change_expert_store_request $request, OrderServiceRepo $orderServiceRepo, UserRepo $userRepo, $orderID, $expertID)
    {
        $order_service = $orderServiceRepo->findByID_check_order_has_pending_cancel_by_team_leader($orderID);
        $this->authorize('change_expert', $order_service);

        $expert = $userRepo->findByID_where_related_team_leader($expertID);
        if ($expert->hasPermissionTo(Permission::PERMISSION_EXPERT)) {
            $order = $this->orderServiceRepo->change_expert_store($order_service, $expertID, $request->message ?? null);
            return updated($order, 'order_service.show', [$orderID], 'done');
        }
        return error();
    }

    public function approved_failed_order_by_team_leader(failed_order_team_leader_request $request, OrderServiceRepo $orderServiceRepo, CancelOrderServiceRepo $cancelOrderServiceRepo, $order_service)
    {
        $order_service = $orderServiceRepo->findByID_check_order_has_pending_cancel_by_team_leader($order_service);
        $this->authorize('approved_failed_order_by_team_leader', $order_service);

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