<?php
ob_start();
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: application/json');

// 检查登录状态和权限
if (!isLoggedIn()) {
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

$user = currentUser();
if ($user['role'] !== 'admin') {
    echo json_encode(['success' => false, 'message' => '权限不足']);
    exit;
}

// 获取参数
$type = trim($_GET['type'] ?? '');

if (empty($type)) {
    echo json_encode(['success' => false, 'message' => '请指定登录类型']);
    exit;
}

// 根据类型检查对应的配置是否存在
$config_ok = false;
$missing = [];

if ($type === 'qq') {
    $app_id = getSiteConfig('oauth_qq_app_id');
    $app_key = getSiteConfig('oauth_qq_app_key');
    
    if (empty($app_id)) $missing[] = 'App ID';
    if (empty($app_key)) $missing[] = 'App Key';
    $config_ok = empty($missing);
} elseif ($type === 'gitee') {
    $client_id = getSiteConfig('oauth_gitee_client_id');
    $client_secret = getSiteConfig('oauth_gitee_client_secret');

    if (empty($client_id)) $missing[] = 'Client ID';
    if (empty($client_secret)) $missing[] = 'Client Secret';
    $config_ok = empty($missing);
    
} else {
    // 其他类型使用彩虹聚合登录配置
    $api_url = getSiteConfig('oauth_union_api_url');
    $app_id = getSiteConfig('oauth_union_app_id');
    $app_key = getSiteConfig('oauth_union_app_key');

    if (empty($api_url)) $missing[] = '接口地址';
    if (empty($app_id)) $missing[] = 'App ID';
    if (empty($app_key)) $missing[] = 'App Key';
    $config_ok = empty($missing);
}

if ($config_ok) {
    echo json_encode(['success' => true, 'message' => '配置完整，可以正常使用']);
} else {
    echo json_encode(['success' => false, 'message' => '配置不完整，缺少：' . implode('、', $missing)]);
}
exit;