const { Sequelize } = require("sequelize");

module.exports = {
  async up(queryInterface) {
    await queryInterface.createTable("student_exercise", {
      ID_STUDENT_EXERCISE: {
        type: Sequelize.UUID,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
      },
      ID_ADMIN_EXERCISE: {
        type: Sequelize.UUID,
        allowNull: false,
      },
      ID_STUDENT_LEARNING: {
        type: Sequelize.UUID,
        allowNull: false,
      },
      ANSWER_STUDENT: {
        type: Sequelize.TEXT,
        allowNull: false,
      },
      IS_CORRECT: {
        type: Sequelize.TINYINT(1),
        allowNull: false,
        defaultValue: 0,
      },
      RESULT_SCORE_STUDENT: {
        type: Sequelize.FLOAT,
        allowNull: true,
        defaultValue: null,
      },
      TIME_STUDENT_EXC: {
        type: Sequelize.DATE,
        allowNull: true,
        defaultValue: Sequelize.NOW,
        onUpdate: Sequelize.NOW,
      },
    });

    await queryInterface.addConstraint("student_exercise", {
      fields: ["ID_ADMIN_EXERCISE"],
      type: "foreign key",
      name: "FK_MENGERJAKAN",
      references: {
        table: "admin_exercise",
        field: "ID_ADMIN_EXERCISE",
      },
      onUpdate: "CASCADE",
      onDelete: "CASCADE",
    });

    await queryInterface.addConstraint("student_exercise", {
      fields: ["ID_STUDENT_LEARNING"],
      type: "foreign key",
      name: "FK_MELIBATKAN",
      references: {
        table: "student_learning",
        field: "ID_STUDENT_LEARNING",
      },
      onUpdate: "CASCADE",
      onDelete: "CASCADE",
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable("student_exercise");
  },
};