<?php

namespace App\Policies;

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

class TaskPolicy
{
    /**
     * Determine whether the user can view any tasks.
     */
    public function viewAny(User $user): bool
    {
        return $user->can('view tasks') || $user->can('manage all tasks');
    }

    /**
     * Determine whether the user can view the task.
     */
    public function view(User $user, Task $task): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('view tasks')) {
            return false;
        }

        return $task->project->users()->where('user_id', $user->id)->exists();
    }

    /**
     * Determine whether the user can create tasks.
     */
    public function create(User $user, $project = null): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('create tasks')) {
            return false;
        }
        if ($project) {
            return $project->users()->where('user_id', $user->id)->exists();
        }

        return true;
    }

    /**
     * Determine whether the user can update the task.
     */
    public function update(User $user, Task $task): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('edit tasks')) {
            return false;
        }

        // Own tasks or tasks in own projects
        return $task->created_by === $user->id
            || $task->project->users()->where('user_id', $user->id)->exists();
    }

    /**
     * Determine whether the user can delete the task.
     */
    public function delete(User $user, Task $task): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('delete tasks')) {
            return false;
        }

        // Only creator or project admin can delete
        return $task->created_by === $user->id
            || $task->project->users()->where('user_id', $user->id)->wherePivot('role_in_project', 'admin')->exists();
    }

    /**
     * Determine whether the user can assign/reassign the task.
     */
    public function assign(User $user, Task $task): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('assign tasks')) {
            return false;
        }

        return $task->project->users()->where('user_id', $user->id)->exists();
    }

    /**
     * Determine whether the user can restore the task.
     */
    public function restore(User $user, Task $task): bool
    {
        if ($user->can('manage all tasks')) {
            return true;
        }
        if (! $user->can('edit tasks')) {
            return false;
        }

        return $task->project->users()->where('user_id', $user->id)->exists();
    }

    /**
     * Determine whether the user can permanently delete the task.
     */
    public function forceDelete(User $user, Task $task): bool
    {
        return $user->can('manage all tasks');
    }
}
