'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('production_cost', {
      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: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      cost_type: {
        type: Sequelize.ENUM('STANDARD', 'ACTUAL', 'ESTIMATED'),
        allowNull: false,
        defaultValue: 'STANDARD',
        comment: 'Type of cost calculation'
      },
      material_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Total material cost'
      },
      labor_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Total labor cost'
      },
      overhead_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Total overhead cost'
      },
      setup_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Setup cost'
      },
      total_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Total production cost'
      },
      quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Quantity for which cost is calculated'
      },
      unit_cost: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Cost per unit'
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      cost_center_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'cost_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Cost center for cost allocation'
      },
      calculated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        comment: 'When cost was calculated'
      },
      calculated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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')
      }
    });

    // Add indexes
    await queryInterface.addIndex('production_cost', ['company_id', 'plant_id'], {
      name: 'production_cost_company_plant_idx'
    });
    await queryInterface.addIndex('production_cost', ['production_order_id'], {
      name: 'production_cost_po_idx'
    });
    await queryInterface.addIndex('production_cost', ['cost_type'], {
      name: 'production_cost_type_idx'
    });
    await queryInterface.addIndex('production_cost', ['cost_center_id'], {
      name: 'production_cost_center_idx'
    });
  },

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

