<?php

namespace App\Models\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;

use App\Models\{Service,User};

class ServicePolicy
{
	use HandlesAuthorization;

	/**
	 * Determine whether the user can view the service.
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @return bool
	 */
	public function view(User $uo, Service $so): bool
	{
		// If this is a service for an account managed by a user.
		return ($uo->services->pluck('id')->search($so->id) !== FALSE)

			// The user is the wholesaler
			OR $uo->isWholesaler()

			// The user has this as one of their accounts
			OR $uo->accounts->pluck('id')->contains($so->account_id);
	}

	/**
	 * Determine whether the user can create services.
	 *
	 * @param User $uo
	 * @return bool
	 */
	public function create(User $uo): bool
	{
		return TRUE;
	}

	/**
	 * Can the user progress an order status
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @param string  $stage
	 * @return bool
	 */
	public function progress(User $uo,Service $so,string $stage=''): bool
	{
		return $stage ? $so->actions()->has(strtoupper($stage)) : $so->actions()->count();
	}

	/**
	 * Determine whether the user can update the service.
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @return bool
	 */
	public function update(User $uo, Service $so): bool
	{
		return $uo->isWholesaler();
	}

	/**
	 * Determine whether the user can delete the service.
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @return bool
	 */
	public function delete(User $uo, Service $so): bool
	{
		return $uo->isWholesaler();
	}

	/**
	 * Determine whether the user can restore the service.
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @return bool
	 */
	public function restore(User $uo, Service $so): bool
	{
		return $uo->isWholesaler();
	}

	/**
	 * Determine whether the user can permanently delete the service.
	 *
	 * @param User    $uo
	 * @param Service $so
	 * @return bool
	 */
	public function forceDelete(User $uo, Service $so): bool
	{
		return $uo->isWholesaler();
	}
}