'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    await queryInterface.createTable('news', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      title: {
        type: Sequelize.STRING,
        allowNull: false,
        comment: '新闻标题'
      },
      content: {
        type: Sequelize.TEXT,
        allowNull: false,
        comment: '新闻内容'
      },
      summary: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '新闻摘要'
      },
      author: {
        type: Sequelize.STRING,
        allowNull: true,
        comment: '作者'
      },
      source: {
        type: Sequelize.STRING,
        allowNull: true,
        comment: '新闻来源'
      },
      coverImage: {
        type: Sequelize.STRING,
        allowNull: true,
        comment: '封面图片URL'
      },
      category: {
        type: Sequelize.STRING,
        allowNull: true,
        comment: '新闻分类'
      },
      tags: {
        type: Sequelize.STRING,
        allowNull: true,
        comment: '标签，用逗号分隔'
      },
      viewCount: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '浏览次数'
      },
      isPublished: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否发布'
      },
      publishedAt: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '发布时间'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false
      }
    });

    // 添加索引
    await queryInterface.addIndex('news', ['title']);
    await queryInterface.addIndex('news', ['category']);
    await queryInterface.addIndex('news', ['isPublished']);
    await queryInterface.addIndex('news', ['publishedAt']);
  },

  async down (queryInterface, Sequelize) {
    await queryInterface.dropTable('news');
  }
};
