'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material', {
      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'
      },
      material_number: {
        type: Sequelize.STRING(40),
        allowNull: false
      },
      material_type: {
        type: Sequelize.ENUM('RAW_MATERIAL', 'SEMI_FINISHED', 'FINISHED_GOOD', 'PACKAGING', 'CONSUMABLE', 'SERVICE', 'ASSET'),
        allowNull: false
      },
      material_group: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: false
      },
      base_uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      gross_weight: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      net_weight: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      weight_uom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      volume: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      volume_uom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      hsn_code: {
        type: Sequelize.STRING(8),
        allowNull: true
      },
      sac_code: {
        type: Sequelize.STRING(6),
        allowNull: true,
        comment: 'Service Accounting Code for services'
      },
      is_batch_managed: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      is_serial_managed: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      is_shelf_life_managed: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      shelf_life_days: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'OBSOLETE', 'BLOCKED'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('material', ['company_id', 'material_number'], { unique: true });
    await queryInterface.addIndex('material', ['material_type']);
    await queryInterface.addIndex('material', ['material_group']);
    await queryInterface.addIndex('material', ['status']);
    await queryInterface.addIndex('material', ['hsn_code']);
    await queryInterface.addIndex('material', ['effective_from', 'effective_to']);
  },

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

