<?php

namespace App\Policies;

use App\Models\Group;
use App\Models\User;

class GroupPolicy
{
    /**
     * 查看分组列表
     */
    public function viewAny(User $user): bool
    {
        return $user->can('group.view');
    }

    /**
     * 查看分组详情
     */
    public function view(User $user, Group $group): bool
    {
        return $user->can('group.view');
    }

    /**
     * 创建分组
     */
    public function create(User $user): bool
    {
        return $user->can('group.create');
    }

    /**
     * 更新分组
     */
    public function update(User $user, Group $group): bool
    {
        return $user->can('group.update');
    }

    /**
     * 删除分组
     */
    public function delete(User $user, Group $group): bool
    {
        // 首先检查权限
        if (!$user->can('group.delete')) {
            return false;
        }

        // 检查是否有关联文档
        if ($group->documents()->count() > 0) {
            return false;
        }

        // 检查是否有关联用户
        if ($group->users()->count() > 0) {
            return false;
        }

        return true;
    }

    /**
     * 批量删除分组
     */
    public function deleteAny(User $user): bool
    {
        return $user->can('group.delete');
    }
}
