'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('wip_tracking', {
      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_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        }
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      wip_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        comment: 'Work in Progress quantity (logical tracking only)'
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      material_value: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0,
        comment: 'Material cost in WIP'
      },
      labor_value: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0,
        comment: 'Labor cost in WIP'
      },
      overhead_value: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0,
        comment: 'Overhead cost in WIP'
      },
      total_wip_value: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0,
        comment: 'Total WIP value (material + labor + overhead)'
      },
      as_on_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'As on date for WIP snapshot'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('wip_tracking', ['company_id', 'plant_id', 'fiscal_year_id']);
    await queryInterface.addIndex('wip_tracking', ['production_order_id']);
    await queryInterface.addIndex('wip_tracking', ['material_id']);
    await queryInterface.addIndex('wip_tracking', ['as_on_date']);
  },

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

