<?php

namespace Renfos\Timesheet\Policies;

use Litepie\User\Contracts\UserPolicy;
use Renfos\Timesheet\Models\Board;

class BoardPolicy
{

    /**
     * Determine if the given user can view the board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function view(UserPolicy $user, Board $board)
    {
        if ($user->canDo('timesheet.board.view') && $user->isAdmin()) {
            return true;
        }

        return $board->user_id == user_id() && $board->user_type == user_type();
    }

    /**
     * Determine if the given user can create a board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function create(UserPolicy $user)
    {
        return  $user->canDo('timesheet.board.create');
    }

    /**
     * Determine if the given user can update the given board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function update(UserPolicy $user, Board $board)
    {
        if ($user->canDo('timesheet.board.edit') && $user->isAdmin()) {
            return true;
        }

        return $board->user_id == user_id() && $board->user_type == user_type();
    }

    /**
     * Determine if the given user can delete the given board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function destroy(UserPolicy $user, Board $board)
    {
        return $board->user_id == user_id() && $board->user_type == user_type();
    }

    /**
     * Determine if the given user can verify the given board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function verify(UserPolicy $user, Board $board)
    {
        if ($user->canDo('timesheet.board.verify')) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the given user can approve the given board.
     *
     * @param UserPolicy $user
     * @param Board $board
     *
     * @return bool
     */
    public function approve(UserPolicy $user, Board $board)
    {
        if ($user->canDo('timesheet.board.approve')) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the user can perform a given action ve.
     *
     * @param [type] $user    [description]
     * @param [type] $ability [description]
     *
     * @return [type] [description]
     */
    public function before($user, $ability)
    {
        if ($user->isSuperuser()) {
            return true;
        }
    }
}