'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('cost_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'
      },
      cost_center_code: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: false
      },
      cost_center_type: {
        type: Sequelize.ENUM('PRODUCTION', 'ADMINISTRATION', 'SALES', 'RESEARCH', 'OTHER'),
        allowNull: true
      },
      parent_cost_center_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'cost_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      department_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'department',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      person_responsible: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', '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('cost_center', ['company_id', 'cost_center_code'], { unique: true });
    await queryInterface.addIndex('cost_center', ['parent_cost_center_id']);
    await queryInterface.addIndex('cost_center', ['status']);
    await queryInterface.addIndex('cost_center', ['effective_from', 'effective_to']);
  },

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

