/**
 * 작업 모델
 *
 * @author TK-FB-Project
 * @since 2026-01-26
 */

const { getDb } = require('../dbPool');

// ==================== 작업 CRUD ====================

/**
 * 작업 생성
 */
const createTask = async (taskData) => {
  const db = await getDb();
  const { work_type_id, task_name, description } = taskData;

  const [result] = await db.query(
    `INSERT INTO tasks (work_type_id, task_name, description, is_active)
     VALUES (?, ?, ?, 1)`,
    [work_type_id || null, task_name, description || null]
  );

  return result.insertId;
};

/**
 * 전체 작업 목록 조회 (공정 정보 포함)
 */
const getAllTasks = async () => {
  const db = await getDb();
  const [rows] = await db.query(
    `SELECT t.task_id, t.work_type_id, t.task_name, t.description, t.is_active,
            t.created_at, t.updated_at,
            wt.name as work_type_name, wt.category
     FROM tasks t
     LEFT JOIN work_types wt ON t.work_type_id = wt.id
     ORDER BY wt.category ASC, t.task_id DESC`
  );
  return rows;
};

/**
 * 활성 작업만 조회
 */
const getActiveTasks = async () => {
  const db = await getDb();
  const [rows] = await db.query(
    `SELECT t.task_id, t.work_type_id, t.task_name, t.description,
            wt.name as work_type_name, wt.category
     FROM tasks t
     LEFT JOIN work_types wt ON t.work_type_id = wt.id
     WHERE t.is_active = 1
     ORDER BY wt.category ASC, t.task_name ASC`
  );
  return rows;
};

/**
 * 공정별 작업 목록 조회 (활성 작업만)
 */
const getTasksByWorkType = async (workTypeId) => {
  const db = await getDb();
  const [rows] = await db.query(
    `SELECT t.task_id, t.work_type_id, t.task_name, t.description, t.is_active,
            t.created_at, t.updated_at,
            wt.name as work_type_name, wt.category
     FROM tasks t
     LEFT JOIN work_types wt ON t.work_type_id = wt.id
     WHERE t.work_type_id = ? AND t.is_active = 1
     ORDER BY t.task_name ASC`,
    [workTypeId]
  );
  return rows;
};

/**
 * 단일 작업 조회
 */
const getTaskById = async (taskId) => {
  const db = await getDb();
  const [rows] = await db.query(
    `SELECT t.task_id, t.work_type_id, t.task_name, t.description, t.is_active,
            t.created_at, t.updated_at,
            wt.name as work_type_name, wt.category
     FROM tasks t
     LEFT JOIN work_types wt ON t.work_type_id = wt.id
     WHERE t.task_id = ?`,
    [taskId]
  );
  return rows[0] || null;
};

/**
 * 작업 수정
 */
const updateTask = async (taskId, taskData) => {
  const db = await getDb();
  const { work_type_id, task_name, description, is_active } = taskData;

  const [result] = await db.query(
    `UPDATE tasks
     SET work_type_id = ?,
         task_name = ?,
         description = ?,
         is_active = ?,
         updated_at = NOW()
     WHERE task_id = ?`,
    [
      work_type_id || null,
      task_name,
      description || null,
      is_active !== undefined ? is_active : 1,
      taskId
    ]
  );

  return result;
};

/**
 * 작업 삭제
 */
const deleteTask = async (taskId) => {
  const db = await getDb();
  const [result] = await db.query(
    `DELETE FROM tasks WHERE task_id = ?`,
    [taskId]
  );
  return result;
};

module.exports = {
  createTask,
  getAllTasks,
  getActiveTasks,
  getTasksByWorkType,
  getTaskById,
  updateTask,
  deleteTask
};
