/**
 * 店铺粉丝增长API路由
 * 作者: 阿瑞
 * 功能: 提供店铺粉丝增长记录的查询和新增接口
 * 版本: 2.0.0
 */

import { NextRequest, NextResponse } from 'next/server';
import { RequestWithDB, connectTeamDB } from '@/lib/db';
import { RowDataPacket } from 'mysql2/promise';

// 定义数据类型接口
interface ShopRecord extends RowDataPacket {
  id: number;
  shop_id: number;
  date: Date;
  total: number;
  deducted: number;
  daily_increase: number;
  created_at: Date;
  updated_at: Date;
  wechat?: string;
  nickname?: string;
}

/**
 * 为请求添加团队信息
 * @param req 原始请求对象
 * @param teamCode 团队代码
 * @returns 包含团队信息的新请求对象
 */
const addTeamInfoToRequest = (req: NextRequest, teamCode: string): NextRequest => {
  // 创建新的请求对象
  const teamReq = new NextRequest(req.url, {
    method: req.method,
    headers: new Headers(req.headers),
    body: req.body,
    cache: req.cache,
    credentials: req.credentials,
    integrity: req.integrity,
    keepalive: req.keepalive,
    mode: req.mode,
    redirect: req.redirect,
    referrer: req.referrer,
    referrerPolicy: req.referrerPolicy
  });
  
  // 添加团队ID信息
  teamReq.headers.set('x-team-id', teamCode);
  
  return teamReq;
};

/**
 * 从请求路径中提取团队代码
 * @param req 请求对象
 * @returns 团队代码
 */
const extractTeamCodeFromPath = (req: NextRequest): string => {
  // 路径格式: /api/team/[teamCode]/shop-follower-growth
  const pathParts = req.nextUrl.pathname.split('/');
  // 团队代码位于索引3的位置
  return pathParts[3] || '';
};

/**
 * 格式化MySQL日期为YYYY-MM-DD格式
 * 修复时区问题，保留原始日期
 */
function formatMySQLDate(date: Date): string {
  // MySQL中存储的是本地日期，直接获取日期部分即可
  // 注意：不使用toISOString()，因为它会转换为UTC时间并导致日期偏移
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

/**
 * GET 查询店铺粉丝增长记录列表
 */
const getGrowthRecords = async (req: RequestWithDB, params?: Record<string, unknown>) => {
  const teamCode = params?.teamCode as string;
  const { searchParams } = new URL(req.url);
  
  // 获取分页参数
  const page = parseInt(searchParams.get('page') || '1');
  const pageSize = parseInt(searchParams.get('pageSize') || '10');
  const offset = (page - 1) * pageSize;
  
  // 获取筛选参数
  const keyword = searchParams.get('keyword');
  const shopId = searchParams.get('shopId');
  const startDate = searchParams.get('startDate');
  const endDate = searchParams.get('endDate');
  
  try {
    console.log(`开始查询团队[${teamCode}]店铺粉丝增长记录`);
    
    // 构建查询条件
    const conditions = [];
    const queryParams: (string | number)[] = [];
    
    if (shopId && parseInt(shopId) > 0) {
      conditions.push('g.shop_id = ?');
      queryParams.push(parseInt(shopId));
    }
    
    if (startDate) {
      conditions.push('g.date >= ?');
      queryParams.push(startDate);
    }
    
    if (endDate) {
      conditions.push('g.date <= ?');
      queryParams.push(endDate);
    }
    
    if (keyword) {
      conditions.push('(s.wechat LIKE ? OR s.nickname LIKE ?)');
      queryParams.push(`%${keyword}%`, `%${keyword}%`);
    }
    
    const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
    
    // 使用数据库连接
    const connection = req.db;
    
    // 查询总记录数
    const [countResult] = await connection.query(
      `SELECT COUNT(*) AS total FROM shop_follower_growth g
       LEFT JOIN shops s ON g.shop_id = s.id
       ${whereClause}`,
      queryParams
    );
    
    const total = (countResult as Array<{ total: number }>)[0].total;
    
    // 查询分页数据
    const paginationParams = [...queryParams, offset, pageSize];
    const [rows] = await connection.query<ShopRecord[]>(
      `SELECT 
         g.id, g.shop_id, g.date, g.total, g.deducted, g.daily_increase,
         g.created_at, g.updated_at,
         s.wechat, s.nickname
       FROM shop_follower_growth g
       LEFT JOIN shops s ON g.shop_id = s.id
       ${whereClause}
       ORDER BY g.date DESC, g.id DESC
       LIMIT ?, ?`,
      paginationParams
    );
    
    // 格式化返回数据
    const records = Array.isArray(rows) ? rows.map(row => ({
      id: row.id,
      shop_id: row.shop_id,
      // 使用日期格式化函数
      date: formatMySQLDate(row.date),
      total: row.total,
      deducted: row.deducted,
      daily_increase: row.daily_increase,
      created_at: row.created_at,
      updated_at: row.updated_at,
      shop_name: row.nickname || row.wechat || `店铺ID: ${row.shop_id}`
    })) : [];
    
    console.log(`查询团队[${teamCode}]店铺粉丝增长记录成功，共 ${total} 条记录`);
    
    return NextResponse.json({
      success: true,
      total,
      records
    });
  } catch (error) {
    console.error(`获取团队[${teamCode}]店铺粉丝增长记录失败:`, error);
    return NextResponse.json(
      { success: false, error: '获取店铺粉丝增长记录失败' },
      { status: 500 }
    );
  }
};

/**
 * POST 添加店铺粉丝增长记录
 */
const addGrowthRecord = async (req: RequestWithDB, params?: Record<string, unknown>) => {
  const teamCode = params?.teamCode as string;
  
  try {
    // 解析请求数据
    const data = await req.json();
    const { shop_id, date, total, deducted } = data;
    
    // 验证必填字段
    if (!shop_id || !date || total === undefined) {
      return NextResponse.json(
        { success: false, error: '店铺ID、日期和粉丝总数为必填项' },
        { status: 400 }
      );
    }
    
    // 验证数据类型
    if (typeof shop_id !== 'number' || typeof total !== 'number') {
      return NextResponse.json(
        { success: false, error: '数据类型错误' },
        { status: 400 }
      );
    }

    // 使用数据库连接
    const connection = req.db;

    // 检查店铺是否存在
    const [shopExists] = await connection.query<RowDataPacket[]>(
      'SELECT id FROM shops WHERE id = ?',
      [shop_id]
    );
    
    if (!Array.isArray(shopExists) || shopExists.length === 0) {
      return NextResponse.json(
        { success: false, error: '店铺不存在' },
        { status: 400 }
      );
    }
    
    // 检查记录是否已存在（同一店铺同一天只能有一条记录）
    const [existingRecord] = await connection.query<RowDataPacket[]>(
      'SELECT id FROM shop_follower_growth WHERE shop_id = ? AND date = ?',
      [shop_id, date]
    );
    
    if (Array.isArray(existingRecord) && existingRecord.length > 0) {
      return NextResponse.json(
        { success: false, error: '该店铺在选定日期已有记录，请编辑现有记录或选择其他日期' },
        { status: 409 }
      );
    }
    
    // 计算日增长
    const daily_increase = total - (deducted || 0);

    // 插入记录
    const [result] = await connection.query(
      `INSERT INTO shop_follower_growth 
       (shop_id, date, total, deducted, daily_increase)
       VALUES (?, ?, ?, ?, ?)`,
      [shop_id, date, total, deducted || 0, daily_increase]
    );
    
    const insertId = (result as { insertId: number }).insertId;
    
    console.log(`添加团队[${teamCode}]店铺粉丝增长记录成功, ID: ${insertId}`);
    
    return NextResponse.json({
      success: true,
      id: insertId,
      shop_id,
      date,
      total,
      deducted: deducted || 0,
      daily_increase
    }, { status: 201 });
  } catch (error) {
    console.error(`添加团队[${teamCode}]店铺粉丝增长记录失败:`, error);
    return NextResponse.json(
      { success: false, error: '添加店铺粉丝增长记录失败' },
      { status: 500 }
    );
  }
};

// 导出处理函数
export async function GET(req: NextRequest) {
  try {
    // 从URL路径中提取团队代码
    const teamCode = extractTeamCodeFromPath(req);
    
    if (!teamCode) {
      return NextResponse.json(
        { success: false, error: '无效的团队代码' },
        { status: 400 }
      );
    }
    
    // 添加团队信息到请求
    const teamReq = addTeamInfoToRequest(req, teamCode);
    
    // 使用新版的connectTeamDB中间件处理请求
    return connectTeamDB(getGrowthRecords)(teamReq, { teamCode });
  } catch (error) {
    console.error('处理店铺粉丝增长记录请求失败:', error);
    return NextResponse.json(
      { success: false, error: '处理请求失败' },
      { status: 500 }
    );
  }
}

export async function POST(req: NextRequest) {
  try {
    // 从URL路径中提取团队代码
    const teamCode = extractTeamCodeFromPath(req);
    
    if (!teamCode) {
      return NextResponse.json(
        { success: false, error: '无效的团队代码' },
        { status: 400 }
      );
    }
    
    // 添加团队信息到请求
    const teamReq = addTeamInfoToRequest(req, teamCode);
    
    // 使用新版的connectTeamDB中间件处理请求
    return connectTeamDB(addGrowthRecord)(teamReq, { teamCode });
  } catch (error) {
    console.error('处理添加店铺粉丝增长记录请求失败:', error);
    return NextResponse.json(
      { success: false, error: '处理请求失败' },
      { status: 500 }
    );
  }
} 