<?php

namespace App\Policies;

use App\Models\Task;
use App\Models\User;

class TaskPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->can('view_tasks');
    }

    public function view(User $user, Task $task): bool
    {
        return $user->can('view_tasks')
            && ($user->canSeeAll('tasks')
                || $task->created_by === $user->id
                || $task->assignees->contains($user->id));
    }

    public function create(User $user): bool
    {
        return $user->can('create_tasks');
    }

    public function update(User $user, Task $task): bool
    {
        return $user->can('update_tasks')
            && ($user->canSeeAll('tasks')
                || $task->created_by === $user->id
                || $task->assignees->contains($user->id));
    }

    public function delete(User $user, Task $task): bool
    {
        return $user->can('delete_tasks')
            && ($user->canSeeAll('tasks') || $task->created_by === $user->id);
    }

    public function deleteAny(User $user): bool
    {
        return $user->can('delete_tasks');
    }
}
