'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('production_schedule', {
      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'
      },
      routing_operation_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'routing_operation',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'work_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      operation_number: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Operation number from routing'
      },
      scheduled_start_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: 'Scheduled start date and time'
      },
      scheduled_end_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: 'Scheduled end date and time'
      },
      planned_hours: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0.00,
        comment: 'Planned hours for this operation'
      },
      scheduled_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Quantity scheduled for this operation'
      },
      priority: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Schedule priority (higher = more urgent)'
      },
      schedule_status: {
        type: Sequelize.ENUM('SCHEDULED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'ON_HOLD'),
        allowNull: false,
        defaultValue: 'SCHEDULED'
      },
      schedule_type: {
        type: Sequelize.ENUM('FORWARD', 'BACKWARD', 'MANUAL'),
        allowNull: false,
        defaultValue: 'FORWARD',
        comment: 'Forward scheduling from start date, backward from due date, or manual'
      },
      remarks: {
        type: Sequelize.TEXT,
        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: {
        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_schedule', ['company_id', 'plant_id'], {
      name: 'production_schedule_company_plant_idx'
    });
    await queryInterface.addIndex('production_schedule', ['production_order_id'], {
      name: 'production_schedule_po_idx'
    });
    await queryInterface.addIndex('production_schedule', ['work_center_id'], {
      name: 'production_schedule_work_center_idx'
    });
    await queryInterface.addIndex('production_schedule', ['scheduled_start_date', 'scheduled_end_date'], {
      name: 'production_schedule_dates_idx'
    });
    await queryInterface.addIndex('production_schedule', ['schedule_status'], {
      name: 'production_schedule_status_idx'
    });
  },

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

