<?php
/**
 * Gitee 账号绑定
 */
require_once dirname(__DIR__) . '/config.php';
require_once dirname(__DIR__) . '/includes/gitee_config.php';

session_start();

// 检查登录
if (!isLoggedIn()) {
    header('Location: ' . SITE_URL . 'modules/home/login.php');
    exit;
}

// 保存绑定标记
$_SESSION['gitee_bind'] = true;
$_SESSION['gitee_bind_user_id'] = $_SESSION['user_id'];

// 生成 state
$state = bin2hex(random_bytes(16));
$_SESSION['gitee_state'] = $state;

// 跳转到 Gitee 授权
$auth_url = 'https://gitee.com/oauth/authorize?' . http_build_query([
    'client_id' => GITEE_CLIENT_ID,
    'redirect_uri' => GITEE_REDIRECT_URI,
    'response_type' => 'code',
    'state' => $state
]);

header('Location: ' . $auth_url);
exit;