<?php

namespace App\Policies;

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

class TaskPolicy
{
    public function view(User $user, Task $task): bool
    {
        if ($user->hasRole('super_admin')) return true;

        return $task->visibility === 'public' ||
               $task->assignees->contains($user->id) ||
               $task->watchers->contains($user->id) ||
               $task->created_by === $user->id;
    }

    public function update(User $user, Task $task): bool
    {
        if ($user->hasRole('super_admin') || $user->hasRole('project_manager')) return true;

        // Developers can update status if assigned
        return $task->assignees->contains($user->id);
    }

    public function delete(User $user, Task $task): bool
    {
        return $user->hasRole('super_admin') || $user->hasRole('project_manager') || $task->created_by === $user->id;
    }
}
