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

module.exports = {
  async up(queryInterface) {
    await queryInterface.createTable("student_learning", {
      ID_STUDENT_LEARNING: {
        type: Sequelize.UUID,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
      },
      ID: {
        type: Sequelize.UUID,
        allowNull: false,
      },
      ID_LEVEL: {
        type: Sequelize.UUID,
        allowNull: false,
      },
      STUDENT_START: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      STUDENT_FINISH: {
        type: Sequelize.DATE,
        allowNull: true,
        defaultValue: null,
      },
      SCORE: {
        type: Sequelize.INTEGER(11),
        allowNull: true,
        defaultValue: null,
      },
      IS_PASS: {
        type: Sequelize.TINYINT(1),
        allowNull: false,
        defaultValue: 0,
      },
      NEXT_LEARNING: {
        type: Sequelize.UUID,
        allowNull: true,
        defaultValue: null,
      },
      FEEDBACK_STUDENT: {
        type: Sequelize.TEXT,
        allowNull: true,
        defaultValue: null,
      },
      TIME_LEARNING: {
        type: Sequelize.DATE,
        allowNull: true,
        defaultValue: Sequelize.NOW,
        onUpdate: Sequelize.NOW,
      },
    });

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

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

    await queryInterface.addConstraint("student_learning", {
      fields: ["NEXT_LEARNING"],
      type: "foreign key",
      name: "FK_MELANJUTKAN",
      references: {
        table: "level",
        field: "ID_LEVEL",
      },
      onUpdate: "CASCADE",
      onDelete: "CASCADE",
    });
  },

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