<?php

namespace App\Models\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;

use App\Models\User;

class UserPolicy
{
	use HandlesAuthorization;

	/**
	 * Determine whether the user can view the user details.
	 *
	 * @param User $uo
	 * @param User $o
	 * @return bool
	 */
	public function view(User $uo,User $o): bool
	{
		// If this is a service for an account managed by a user.
		return ($uo->id == $o->id)

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

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

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

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

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

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

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