'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_order', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      production_order_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      production_order_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      finished_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      bom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'bom_header',
          key: 'id'
        }
      },
      routing_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'routing',
          key: 'id'
        }
      },
      order_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      start_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      finish_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      confirmed_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      scrapped_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'RELEASED', 'IN_PROGRESS', 'PARTIALLY_CONFIRMED', 'CONFIRMED', 'CLOSED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      released_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      released_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      closed_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      closed_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('production_order', ['company_id', 'plant_id', 'fiscal_year_id']);
    await queryInterface.addIndex('production_order', ['production_order_number']);
    await queryInterface.addIndex('production_order', ['production_order_date']);
    await queryInterface.addIndex('production_order', ['finished_material_id']);
    await queryInterface.addIndex('production_order', ['status']);
  },

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

