<?php

namespace Sma\Transaction\Policies;

use App\User;
use Sma\Transaction\Models\Transaction;

class TransactionPolicy
{

    /**
     * Determine if the given user can view the transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function view(User $user, Transaction $transaction)
    {
        if ($user->canDo('transaction.transaction.view') && $user->is('admin')) {
            return true;
        }

        if ($user->canDo('blocks.block.view') 
        && $user->is('manager')
        && $block->user->parent_id == $user->id) {
            return true;
        }

        return $user->id === $transaction->user_id;
    }

    /**
     * Determine if the given user can create a transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function create(User $user)
    {
        return  $user->canDo('transaction.transaction.create');
    }

    /**
     * Determine if the given user can update the given transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function update(User $user, Transaction $transaction)
    {
        if ($user->canDo('transaction.transaction.update') && $user->is('admin')) {
            return true;
        }

        if ($user->canDo('blocks.block.update') 
        && $user->is('manager')
        && $block->user->parent_id == $user->id) {
            return true;
        }

        return $user->id === $transaction->user_id;
    }

    /**
     * Determine if the given user can delete the given transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function destroy(User $user, Transaction $transaction)
    {
        if ($user->canDo('transaction.transaction.delete') && $user->is('admin')) {
            return true;
        }

        if ($user->canDo('blocks.block.delete') 
        && $user->is('manager')
        && $block->user->parent_id == $user->id) {
            return true;
        }

        return $user->id === $transaction->user_id;
    }

    /**
     * Determine if the given user can verify the given transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function verify(User $user, Transaction $transaction)
    {
        if ($user->canDo('transaction.transaction.verify') && $user->is('admin')) {
            return true;
        }

        if ($user->canDo('transaction.transaction.verify') 
        && $user->is('manager')
        && $transaction->user->parent_id == $user->id) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the given user can approve the given transaction.
     *
     * @param User $user
     * @param Transaction $transaction
     *
     * @return bool
     */
    public function approve(User $user, Transaction $transaction)
    {
        if ($user->canDo('transaction.transaction.approve') && $user->is('admin')) {
            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;
        }
    }
}