<?php

namespace Mehrab\OrderService\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;
use Mehrab\OrderService\Models\OrderService;
use Mehrab\RolePermission\Models\Permission;
use Mehrab\User\Models\User;

class OrderServicePolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function index(User $user)
    {
        return $user->hasAnyPermission([Permission::PERMISSION_ORDER_SERVICE_OPERATOR, Permission::PERMISSION_EXPERT, Permission::PERMISSION_TEAM_LEADER,Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE,Permission::PERMISSION_ORDER_SERVICE_FACTOR]);
    }

    public function show(User $user, OrderService $order)
    {
        if ($user->hasPermissionTo(Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE)){return true;}

        return $user->hasAnyPermission([Permission::PERMISSION_ORDER_SERVICE_OPERATOR, Permission::PERMISSION_EXPERT, Permission::PERMISSION_TEAM_LEADER]) &&
            $order->check_access();
    }

    public function choose_team_leader_by_operator(User $user, OrderService $order)
    {
        if ($user->hasPermissionTo(Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE)){return true;}

        return $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_OPERATOR) &&
            auth()->id() == $order->operator->id;
    }

    public function all_order_service_manager_cancel(User $user){
        return $user->hasPermissionTo(Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE);
    }

    public function change_team_leader_by_all_order_service_manager(User $user)
    {
        return $user->hasPermissionTo(Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE);
    }


    public function operator_cancel_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_OPERATOR) &&
            auth()->id() == $order->operator->id;
    }

    public function change_team_leader(User $user, OrderService $order)
    {
        return $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_OPERATOR) &&
            auth()->id() == $order->operator->id;
    }

    public function approved_failed_order_by_operator(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_OPERATOR) &&
            auth()->id() == $order->operator->id;
    }

    public function choose_expert_by_team_leader(User $user, OrderService $order)
    {
        return $user->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER) &&
            auth()->id() == $order->team_leader->id;
    }

    public function team_leader_cancel_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER) &&
            auth()->id() == $order->team_leader->id;
    }

    public function change_expert(User $user, OrderService $order)
    {
        return $user->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER) &&
            auth()->id() == $order->team_leader->id;
    }

    public function approved_failed_order_by_team_leader(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_TEAM_LEADER) &&
            auth()->id() == $order->team_leader->id;
    }

    public function expert_cancel_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $order->expert_id;
    }

    public function expert_accept_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $order->expert_id;
    }

    public function expert_start_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $order->expert_id;
    }

    public function expert_not_agreement_with_customer(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $order->expert_id;
    }

    public function expert_end_order_service(User $user,OrderService $order){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $order->expert_id;
    }

    public function set_factor_order_by_factor_admin(User $user){
        return $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_FACTOR);
    }

    public function set_order(User $user){
        return $user->hasPermissionTo(Permission::PERMISSION_USER);
    }

    public function order_user(User $user,OrderService $orderService){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $orderService->user_id;
    }

    public function do_payment(User $user,OrderService $orderService){
        return $user->hasPermissionTo(Permission::PERMISSION_EXPERT) &&
            auth()->id() == $orderService->user_id;
    }



}
