'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('machine', {
      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',
        comment: 'Company this machine belongs to'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Plant location (optional)'
      },
      machine_code: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Machine code (e.g., LSR 01, PBR 02)'
      },
      machine_description: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Machine description/name'
      },
      specification: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Machine specifications'
      },
      make: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Machine manufacturer/make'
      },
      installed_on: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Year when machine was installed'
      },
      history_card_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'History card number (e.g., HC-07)'
      },
      location: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Location/Plant name (e.g., SARA INDUSTRIES, RAM INDUSTRIES)'
      },
      status: {
        type: Sequelize.ENUM('WORKING', 'NOT_WORKING', 'MAINTENANCE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'WORKING',
        comment: 'Machine status'
      },
      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')
      }
    });

    // Create unique index on company_id and machine_code
    await queryInterface.addIndex('machine', ['company_id', 'machine_code'], {
      unique: true,
      name: 'machine_company_machine_code_unique'
    });

    // Create index on company_id
    await queryInterface.addIndex('machine', ['company_id'], {
      name: 'machine_company_id_idx'
    });

    // Create index on plant_id
    await queryInterface.addIndex('machine', ['plant_id'], {
      name: 'machine_plant_id_idx'
    });

    // Create index on status
    await queryInterface.addIndex('machine', ['status'], {
      name: 'machine_status_idx'
    });

    // Create index on location
    await queryInterface.addIndex('machine', ['location'], {
      name: 'machine_location_idx'
    });
  },

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

