/**
 * 团队数据库初始化API
 * 作者: 阿瑞
 * 功能: 初始化团队专用数据库
 * 版本: 1.2.0
 */

import { NextRequest, NextResponse } from "next/server";
import { TeamModel } from "@/models/team";
import { AuthUtils } from "@/lib/auth";
import mysql from 'mysql2/promise';
import DbConfig from "@/lib/db/config";
import { TeamDBInitializer } from "@/lib/db/init-team-db";
import { ITeam } from "@/models/system/types";

/**
 * 团队数据库处理类
 */
class TeamDatabase {
  private config: {
    db_host: string;
    db_name: string;
    db_username: string;
    db_password: string;
    team_code: string;
  };

  constructor(config: {
    db_host: string;
    db_name: string;
    db_username: string;
    db_password: string;
    team_code: string;
  }) {
    this.config = config;
  }

  /**
   * 初始化数据库
   */
  async initialize(): Promise<void> {
    // 连接到数据库
    console.log(`连接到团队数据库: ${this.config.db_name}`);
    const connection = await mysql.createConnection({
      host: this.config.db_host,
      user: this.config.db_username,
      password: this.config.db_password,
      database: this.config.db_name
    });

    try {
      // 使用TeamDBInitializer初始化表结构
      await TeamDBInitializer.initialize(connection);
      
      // 初始化默认数据
      await TeamDBInitializer.initDefaultData(connection);
    } catch (error) {
      console.error(`团队 ${this.config.team_code} 数据库初始化失败:`, error);
      throw error;
    } finally {
      // 确保关闭连接
      await connection.end();
    }
  }
}

/**
 * POST 处理初始化团队数据库请求
 * 符合Next.js 15.3+的路由处理器签名
 */
export async function POST(request: NextRequest) {
  try {
    // 从URL路径中提取团队代码
    const pathname = request.nextUrl.pathname;
    const teamCode = pathname.split('/')[3]; // 路径格式: /api/teams/[teamCode]/init-database
    
    console.log(`开始初始化团队数据库, 团队代码: ${teamCode}`);

    // 从请求头获取访问令牌
    const authHeader = request.headers.get('Authorization');
    let token = '';

    if (authHeader && authHeader.startsWith('Bearer ')) {
      token = authHeader.substring(7);
    } else {
      // 尝试从Cookie中获取
      const tokenCookie = request.cookies.get('accessToken');
      if (tokenCookie) {
        token = tokenCookie.value;
      } else {
        // 尝试读取localStorage中的令牌（从请求中）
        const saasStorage = request.cookies.get('saas-user-storage');
        if (saasStorage) {
          try {
            const storageData = JSON.parse(decodeURIComponent(saasStorage.value));
            if (storageData && storageData.state && storageData.state.accessToken) {
              token = storageData.state.accessToken;
            }
          } catch (e) {
            console.error('解析存储的用户数据失败:', e);
          }
        }
      }
    }

    // 验证令牌并获取用户信息
    if (!token) {
      console.error('初始化数据库失败: 用户未登录');
      return NextResponse.json(
        { success: false, error: "用户未登录" },
        { status: 401 }
      );
    }

    // 验证用户身份
    const user = await AuthUtils.verifyToken(token);
    if (!user || !user.id) {
      return NextResponse.json(
        { success: false, error: "用户验证失败" },
        { status: 401 }
      );
    }

    // 获取团队信息
    const team = await TeamModel.getByTeamCode(teamCode) as ITeam;
    if (!team) {
      console.error(`团队不存在, 团队代码: ${teamCode}`);
      return NextResponse.json(
        { success: false, error: "团队不存在" },
        { status: 404 }
      );
    }

    console.log(`获取到团队信息: ${team.name}, ID: ${team.id}`);

    // 检查用户权限（必须是团队所有者）
    const userId = Number(user.id);
    if (team.owner_id !== userId) {
      console.error(`权限错误: 用户(${userId})不是团队(${team.id})的所有者(${team.owner_id})`);
      return NextResponse.json(
        { success: false, error: "只有团队所有者可以初始化数据库" },
        { status: 403 }
      );
    }

    try {
      // 确保团队对象包含所需的数据库信息
      if (!team.db_name || !team.db_username || !team.db_password || !team.db_host) {
        console.error('团队数据库信息不完整');
        return NextResponse.json(
          { success: false, error: "团队数据库信息不完整" },
          { status: 500 }
        );
      }

      // 首先使用管理员连接
      console.log(`使用管理员账号连接到数据库: ${DbConfig.host}`);
      const rootConnection = await mysql.createConnection({
        host: DbConfig.host,
        user: DbConfig.adminUser,
        password: DbConfig.adminPassword
      });

      // 查询数据库是否存在
      const [existingDbs] = await rootConnection.query(
        `SHOW DATABASES LIKE '${team.db_name}'`
      );

      // 如果数据库不存在，创建数据库
      if (Array.isArray(existingDbs) && existingDbs.length === 0) {
        console.log(`创建团队数据库: ${team.db_name}`);
        await rootConnection.query(
          `CREATE DATABASE IF NOT EXISTS \`${team.db_name}\` 
           CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`
        );
      } else {
        console.log(`团队数据库 ${team.db_name} 已存在`);
      }

      // 创建或更新数据库用户
      console.log(`创建/更新团队数据库用户: ${team.db_username}`);

      // 删除可能存在的同名用户（确保用户不存在）
      try {
        await rootConnection.query(`DROP USER IF EXISTS '${team.db_username}'@'%'`);
        await rootConnection.query(`DROP USER IF EXISTS '${team.db_username}'@'localhost'`);
      } catch (e) {
        console.log(`删除已存在用户失败，可能是用户不存在: ${e}`);
      }

      // 创建新用户（同时为本地和远程连接创建）
      await rootConnection.query(
        `CREATE USER '${team.db_username}'@'localhost' IDENTIFIED BY '${team.db_password}'`
      );
      await rootConnection.query(
        `CREATE USER '${team.db_username}'@'%' IDENTIFIED BY '${team.db_password}'`
      );

      // 授予用户对该数据库的所有权限
      await rootConnection.query(
        `GRANT ALL PRIVILEGES ON \`${team.db_name}\`.* TO '${team.db_username}'@'localhost'`
      );
      await rootConnection.query(
        `GRANT ALL PRIVILEGES ON \`${team.db_name}\`.* TO '${team.db_username}'@'%'`
      );

      // 刷新权限表
      await rootConnection.query('FLUSH PRIVILEGES');

      console.log(`用户 ${team.db_username} 创建完成并授权成功`);

      // 关闭管理员连接
      await rootConnection.end();

      // 初始化团队数据库表结构
      console.log(`使用团队专用账号连接并初始化数据库表结构: ${team.db_name}`);
      const teamDatabase = new TeamDatabase({
        db_host: team.db_host,
        db_name: team.db_name,
        db_username: team.db_username,
        db_password: team.db_password,
        team_code: team.team_code
      });

      // 执行初始化
      await teamDatabase.initialize();
      console.log(`团队数据库 ${team.db_name} 初始化成功`);

    } catch (dbError: unknown) {
      const err = dbError as Error;
      console.error("数据库操作失败:", err);
      return NextResponse.json(
        { success: false, error: `数据库操作失败: ${err.message}` },
        { status: 500 }
      );
    }

    // 返回成功响应
    return NextResponse.json({
      success: true,
      message: `团队 ${team.name} 数据库初始化成功`
    });

  } catch (error: unknown) {
    const err = error as Error;
    console.error("初始化团队数据库出错:", err);
    return NextResponse.json(
      { success: false, error: err.message || "初始化团队数据库失败" },
      { status: 500 }
    );
  }
}