<?php
/**
 * 飞书账号绑定
 */
require_once dirname(__DIR__) . '/config.php';
require_once dirname(__DIR__) . '/includes/feishu_config.php';

session_start();

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

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

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

// 跳转到飞书授权
$auth_url = 'https://open.feishu.cn/open-apis/authen/v1/authorize?' . http_build_query([
    'app_id' => FEISHU_APP_ID,
    'redirect_uri' => FEISHU_REDIRECT_URI,
    'state' => $state
]);

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