"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("ForumPosts", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      authorId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      title: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      content: {
        type: Sequelize.TEXT,
        allowNull: false,
      },
      category: {
        type: Sequelize.ENUM(
          "item_request",
          "technical_support",
          "community_resources",
          "general_discussion"
        ),
        allowNull: false,
        defaultValue: "general_discussion",
      },
      status: {
        type: Sequelize.ENUM("open", "answered", "closed"),
        defaultValue: "open",
      },
      viewCount: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
      },
      commentCount: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
      },
      zipCode: {
        type: Sequelize.STRING,
      },
      latitude: {
        type: Sequelize.DECIMAL(10, 8),
      },
      longitude: {
        type: Sequelize.DECIMAL(11, 8),
      },
      acceptedAnswerId: {
        type: Sequelize.UUID,
        allowNull: true,
      },
      images: {
        type: Sequelize.ARRAY(Sequelize.TEXT),
        allowNull: true,
        defaultValue: [],
      },
      isPinned: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      isDeleted: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      deletedBy: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "SET NULL",
      },
      deletedAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      deletionReason: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      closedBy: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "SET NULL",
      },
      closedAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // Add indexes
    await queryInterface.addIndex("ForumPosts", ["authorId"]);
    await queryInterface.addIndex("ForumPosts", ["category"]);
    await queryInterface.addIndex("ForumPosts", ["status"]);
    await queryInterface.addIndex("ForumPosts", ["isDeleted"]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable("ForumPosts");
  },
};
