<?php

namespace App\Policies;

use App\Models\Milestone;
use App\Models\ServiceOrder;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Nova;

class MilestonePolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function viewAny(User $user)
    {
        return Nova::whenServing(function (NovaRequest $request) use ($user) {
            return $user->hasAnyPermission(['control projects']);
        });
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Milestone  $milestone
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(User $user, Milestone $milestone)
    {
        return Nova::whenServing(function (NovaRequest $request) use ($user) {
            return $user->hasAnyPermission(['control projects']);
        }, function (Request $request) use ($user,$milestone) {
            if($milestone->milestonable() instanceof ServiceOrder){
                if($milestone->milestonable->user?->id === $user->id || ($milestone->milestonable->service->serviceable->user->id === $user->id)){
                    return true;
                }else{
                    return url()->previous();
                }
            }
            else if($milestone->milestonable->project?->user?->id === $user->id ||($milestone->milestonable->project->projectable->user->id === $user->id)) {
                return true;
            }
            return url()->previous();
        });
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function create(User $user)
    {
        if($user->engineer() !=null || $user->firm() !=null)
        {
            return true;
        }else{
            return url()->previous();
        }
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Milestone  $milestone
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(User $user, Milestone $milestone)
    {
        //
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Milestone  $milestone
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(User $user, Milestone $milestone)
    {
        //
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Milestone  $milestone
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function restore(User $user, Milestone $milestone)
    {
        return Nova::whenServing(function (NovaRequest $request) use ($user) {
            return $user->hasAnyPermission(['control projects']);
        });
    }

    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Milestone  $milestone
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function forceDelete(User $user, Milestone $milestone)
    {
        return Nova::whenServing(function (NovaRequest $request) use ($user) {
            return $user->hasAnyPermission(['control projects']);
        });
    }
}
