/**
 * TBM 세션에 공정/작업 컬럼 추가
 *
 * @param {import('knex').Knex} knex
 */
exports.up = function(knex) {
  return knex.schema.table('tbm_sessions', function(table) {
    table.integer('work_type_id').nullable().comment('공정 ID (work_types 참조)');
    table.integer('task_id').unsigned().nullable().comment('작업 ID (tasks 참조)');

    // 외래키 추가
    table.foreign('work_type_id')
      .references('id')
      .inTable('work_types')
      .onDelete('SET NULL')
      .onUpdate('CASCADE');

    table.foreign('task_id')
      .references('task_id')
      .inTable('tasks')
      .onDelete('SET NULL')
      .onUpdate('CASCADE');

    // 인덱스 추가
    table.index('work_type_id');
    table.index('task_id');
  }).then(() => {
  });
};

/**
 * @param {import('knex').Knex} knex
 */
exports.down = function(knex) {
  return knex.schema.table('tbm_sessions', function(table) {
    table.dropForeign('work_type_id');
    table.dropForeign('task_id');
    table.dropColumn('work_type_id');
    table.dropColumn('task_id');
  });
};
