/**
 * 迁移: 初始化数据库结构（确保所有表和字段存在）
 * 版本: 0000
 * 日期: 2025-01-09
 */

module.exports = {
  async up(manager) {
    console.log('  - 检查并创建所有必需的表和字段...');
    
    // Activities 表的额外字段
    const activityFields = [
      { name: 'registrationEnabled', sql: 'ALTER TABLE Activities ADD COLUMN registrationEnabled INTEGER DEFAULT 1;' },
      { name: 'coverImage', sql: 'ALTER TABLE Activities ADD COLUMN coverImage TEXT;' },
      { name: 'price', sql: 'ALTER TABLE Activities ADD COLUMN price DECIMAL(10, 2) DEFAULT 0;' },
      { name: 'budget', sql: 'ALTER TABLE Activities ADD COLUMN budget DECIMAL(10, 2) DEFAULT 0;' },
      { name: 'locationLat', sql: 'ALTER TABLE Activities ADD COLUMN locationLat DECIMAL(10, 7);' },
      { name: 'locationLng', sql: 'ALTER TABLE Activities ADD COLUMN locationLng DECIMAL(10, 7);' }
    ];
    
    for (const field of activityFields) {
      await manager.executeSql(field.sql);
    }
    
    // Registrations 表的额外字段
    const registrationFields = [
      { name: 'peopleCount', sql: 'ALTER TABLE Registrations ADD COLUMN peopleCount INTEGER DEFAULT 1;' },
      { name: 'note', sql: 'ALTER TABLE Registrations ADD COLUMN note TEXT;' }
    ];
    
    for (const field of registrationFields) {
      await manager.executeSql(field.sql);
    }
    
    // Expenses 表的额外字段
    const expenseFields = [
      { name: 'category', sql: 'ALTER TABLE Expenses ADD COLUMN category TEXT;' },
      { name: 'receipt', sql: 'ALTER TABLE Expenses ADD COLUMN receipt TEXT;' },
      { name: 'note', sql: 'ALTER TABLE Expenses ADD COLUMN note TEXT;' },
      { name: 'media', sql: 'ALTER TABLE Expenses ADD COLUMN media TEXT;' }
    ];
    
    for (const field of expenseFields) {
      await manager.executeSql(field.sql);
    }
    
    // Photos 表的额外字段
    const photoFields = [
      { name: 'description', sql: 'ALTER TABLE Photos ADD COLUMN description TEXT;' },
      { name: 'uploadedBy', sql: 'ALTER TABLE Photos ADD COLUMN uploadedBy TEXT;' },
      { name: 'status', sql: 'ALTER TABLE Photos ADD COLUMN status TEXT DEFAULT "approved";' }
    ];
    
    for (const field of photoFields) {
      await manager.executeSql(field.sql);
    }
    
    // Sponsors 表的额外字段
    const sponsorFields = [
      { name: 'logo', sql: 'ALTER TABLE Sponsors ADD COLUMN logo TEXT;' },
      { name: 'type', sql: 'ALTER TABLE Sponsors ADD COLUMN type TEXT DEFAULT "sponsor";' },
      { name: 'link', sql: 'ALTER TABLE Sponsors ADD COLUMN link TEXT;' }
    ];
    
    for (const field of sponsorFields) {
      await manager.executeSql(field.sql);
    }
    
    // Admins 表的额外字段
    const adminFields = [
      { name: 'name', sql: 'ALTER TABLE Admins ADD COLUMN name TEXT;' }
    ];
    
    for (const field of adminFields) {
      await manager.executeSql(field.sql);
    }
    
    console.log('  - 初始化完成');
  },

  async down(manager) {
    console.log('  - 回滚不支持');
  }
};
