'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('production_costing', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      finished_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      bom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'bom_header',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      routing_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'routing',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      material_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000
      },
      operation_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000
      },
      total_production_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000
      },
      material_cost_lines_json: {
        type: Sequelize.TEXT('long'),
        allowNull: true
      },
      operation_cost_lines_json: {
        type: Sequelize.TEXT('long'),
        allowNull: true
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      estimated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      estimated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('production_costing', ['company_id', 'plant_id'], {
      name: 'production_costing_company_plant_idx'
    });
    await queryInterface.addIndex('production_costing', ['finished_material_id'], {
      name: 'production_costing_material_idx'
    });
    await queryInterface.addIndex('production_costing', ['estimated_at'], {
      name: 'production_costing_estimated_at_idx'
    });
  },

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