'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('work_center', {
      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'
      },
      work_center_code: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: false
      },
      work_center_category: {
        type: Sequelize.ENUM('MACHINE', 'LABOR', 'PRODUCTION_LINE', 'ASSEMBLY_LINE', 'OTHER'),
        allowNull: false,
        defaultValue: 'MACHINE'
      },
      cost_center_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'cost_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      capacity_category: {
        type: Sequelize.STRING(2),
        allowNull: true,
        comment: 'Capacity category code'
      },
      number_of_machines: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 1
      },
      number_of_persons: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 1
      },
      capacity: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        defaultValue: 0.00,
        comment: 'Capacity per day in hours'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'MAINTENANCE'),
        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('work_center', ['company_id', 'plant_id', 'work_center_code'], { unique: true });
    await queryInterface.addIndex('work_center', ['plant_id']);
    await queryInterface.addIndex('work_center', ['status']);
    await queryInterface.addIndex('work_center', ['effective_from', 'effective_to']);
  },

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

