<?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\ExpertsRequests\failed_order_expert_request;
use Mehrab\OrderService\Http\Requests\ExpertsRequests\set_factor_by_expert_request;
use Mehrab\OrderService\Http\Requests\OperatorsRequests\choose_team_leader_store_request;
use Mehrab\OrderService\Http\Requests\OperatorsRequests\failed_order_operator_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\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\ServiceMetaRepo;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\User\Helpers\SmsFactorMethod;
use Mehrab\User\Repositories\UserRepo;
use SanjabVerify\Support\Facades\Verify;


class FactorAdminOrderServiceController extends Controller
{
    private $orderServiceRepo;

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

    public function set_factor_index(OrderServiceRepo $orderServiceRepo,ServiceMetaRepo $serviceMetaRepo,$orderId){

        $order = $orderServiceRepo->findByID_check_order_status_be_related_factor_admin($orderId);
        $this->authorize('set_factor_order_by_factor_admin', $order);
//        dd($order->meta);
        $prices = $serviceMetaRepo->get_all_prices_service_list($order->service_id);
        return view('OrderService::Admin.FactorAdmin.end_work_factor_admin_order_service', compact('order', 'prices'));

    }

    public function set_factor_order_by_factor_admin(Request $request,OrderServiceRepo $orderServiceRepo,$orderId){
        $this->validate($request,[
            'price_final' => ['required','numeric','min:0']
        ]);
        $order = $orderServiceRepo->findByID_check_order_status_be_related_factor_admin($orderId);
        $this->authorize('set_factor_order_by_factor_admin', $order);

        $order_update = $orderServiceRepo->end_work_order_by_order_admin_set_factor($request,$order);
        $message = 'فاکتور این سفارش توسط مدیر فاکتور ' . auth()->id() . 'ثبت شد';
        $factor = $orderServiceRepo->update_reports($order,OrderService::WAITING_PAYMENT,'',$message,[$order->team_leader_id,$order->operator_id,$order->expert_id]);
        $mobile=$order->user->mobile;
        if($mobile){
            $username=$order->user->name?? $mobile;
            $result =resolve(SmsFactorMethod::class)->send($mobile,$username);
        }
        session()->flash('status','updated');

    }
}
