#!/usr/bin/env tsx
/**
 * 检查数据库中实际存储的时间格式
 */
import { initDatabase } from '../models/index.js';
import { AppDataSource } from '../models/index.js';
import { logger } from '../utils/logger.js';

async function checkDbTimestamp() {
  try {
    await initDatabase();
    logger.info('数据库连接已初始化');

    // 检查 works 表
    logger.info('\n检查 works 表的时间格式:');
    const worksResult = await AppDataSource.query(`
      SELECT 
        id, 
        title,
        created_at,
        updated_at,
        DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as created_at_formatted,
        DATE_FORMAT(updated_at, '%Y-%m-%d %H:%i:%s') as updated_at_formatted
      FROM works 
      ORDER BY id DESC 
      LIMIT 3
    `);
    
    worksResult.forEach((row: any) => {
      logger.info(`\n作品 ID: ${row.id}`);
      logger.info(`标题: ${row.title}`);
      logger.info(`created_at (原始): ${row.created_at}`);
      logger.info(`created_at (格式化): ${row.created_at_formatted}`);
      logger.info(`updated_at (原始): ${row.updated_at}`);
      logger.info(`updated_at (格式化): ${row.updated_at_formatted}`);
    });

    // 检查 work_day_statistics 表
    logger.info('\n检查 work_day_statistics 表的时间格式:');
    const statsResult = await AppDataSource.query(`
      SELECT 
        id,
        created_at,
        updated_at,
        DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as created_at_formatted,
        DATE_FORMAT(updated_at, '%Y-%m-%d %H:%i:%s') as updated_at_formatted
      FROM work_day_statistics 
      ORDER BY id DESC 
      LIMIT 3
    `);
    
    statsResult.forEach((row: any) => {
      logger.info(`\n统计 ID: ${row.id}`);
      logger.info(`created_at (原始): ${row.created_at}`);
      logger.info(`created_at (格式化): ${row.created_at_formatted}`);
      logger.info(`updated_at (原始): ${row.updated_at}`);
      logger.info(`updated_at (格式化): ${row.updated_at_formatted}`);
    });

    logger.info('\n✅ 检查完成！');

  } catch (error: any) {
    logger.error('检查失败:', error);
    if (error instanceof Error) {
      logger.error('错误堆栈:', error.stack);
    }
    process.exit(1);
  } finally {
    await AppDataSource.destroy();
    process.exit(0);
  }
}

checkDbTimestamp().catch(console.error);
