<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('users.manage');

require_once __DIR__ . '/../config/database.php';

requirePermission('manage_tasks');

header('Content-Type: application/json; charset=utf-8');

$companyId = isset($_GET['company_id']) ? (int)$_GET['company_id'] : 0;

if ($companyId <= 0) {
    echo json_encode([]);
    exit;
}

/**
 * We return users linked to that company.
 * Assumption: users table has company_id column.
 * If your users table uses a different column name, tell me the column name and I’ll adjust.
 */
try {
    $stmt = $pdo->prepare("
        SELECT id, full_name
        FROM users
        WHERE company_id = ?
          AND status = 'ACTIVE'
        ORDER BY full_name ASC
    ");
    $stmt->execute([$companyId]);
    echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
} catch (Exception $e) {
    echo json_encode([]);
}