/**
 * Vacation Controller
 *
 * 휴가 유형 + 연차 배정 관리
 * Sprint 001: sso_users 기반 전환 + 장기근속
 */

const vacationModel = require('../models/vacationModel');

/* ===== Vacation Types ===== */

async function getVacationTypes(req, res, next) {
  try {
    const all = req.query.all === 'true';
    const data = all ? await vacationModel.getAllVacationTypes() : await vacationModel.getVacationTypes();
    res.json({ success: true, data });
  } catch (err) { next(err); }
}

async function createVacationType(req, res, next) {
  try {
    const { type_code, type_name } = req.body;
    if (!type_code || !type_name) return res.status(400).json({ success: false, error: '유형 코드와 이름은 필수입니다' });
    const data = await vacationModel.createVacationType(req.body);
    res.status(201).json({ success: true, data });
  } catch (err) {
    if (err.code === 'ER_DUP_ENTRY') return res.status(409).json({ success: false, error: '이미 존재하는 유형 코드입니다' });
    next(err);
  }
}

async function updateVacationType(req, res, next) {
  try {
    const data = await vacationModel.updateVacationType(parseInt(req.params.id), req.body);
    if (!data) return res.status(404).json({ success: false, error: '휴가 유형을 찾을 수 없습니다' });
    res.json({ success: true, data });
  } catch (err) {
    if (err.code === 'ER_DUP_ENTRY') return res.status(409).json({ success: false, error: '이미 존재하는 유형 코드입니다' });
    next(err);
  }
}

async function deleteVacationType(req, res, next) {
  try {
    await vacationModel.deleteVacationType(parseInt(req.params.id));
    res.json({ success: true, message: '휴가 유형이 비활성화되었습니다' });
  } catch (err) { next(err); }
}

async function updatePriorities(req, res, next) {
  try {
    const { items } = req.body;
    if (!items || !Array.isArray(items)) return res.status(400).json({ success: false, error: 'items 배열이 필요합니다' });
    await vacationModel.updatePriorities(items);
    res.json({ success: true, message: '우선순위가 업데이트되었습니다' });
  } catch (err) { next(err); }
}

/* ===== Vacation Balances ===== */

async function getBalancesByYear(req, res, next) {
  try {
    const year = parseInt(req.params.year);
    const data = await vacationModel.getBalancesByYear(year);
    res.json({ success: true, data });
  } catch (err) { next(err); }
}

async function getBalancesByUserYear(req, res, next) {
  try {
    const userId = parseInt(req.params.userId);
    const year = parseInt(req.params.year);
    const data = await vacationModel.getBalancesByUserYear(userId, year);
    res.json({ success: true, data });
  } catch (err) { next(err); }
}

async function createBalance(req, res, next) {
  try {
    const { user_id, vacation_type_id, year } = req.body;
    if (!user_id || !vacation_type_id || !year) {
      return res.status(400).json({ success: false, error: '사용자, 휴가유형, 연도는 필수입니다' });
    }
    const data = await vacationModel.createBalance({ ...req.body, created_by: req.user.user_id });
    res.status(201).json({ success: true, data });
  } catch (err) { next(err); }
}

async function updateBalance(req, res, next) {
  try {
    const data = await vacationModel.updateBalance(parseInt(req.params.id), req.body);
    if (!data) return res.status(404).json({ success: false, error: '배정 정보를 찾을 수 없습니다' });
    res.json({ success: true, data });
  } catch (err) { next(err); }
}

async function deleteBalance(req, res, next) {
  try {
    await vacationModel.deleteBalance(parseInt(req.params.id));
    res.json({ success: true, message: '삭제되었습니다' });
  } catch (err) { next(err); }
}

async function bulkUpsertBalances(req, res, next) {
  try {
    const { balances } = req.body;
    if (!balances || !Array.isArray(balances)) return res.status(400).json({ success: false, error: 'balances 배열이 필요합니다' });
    const items = balances.map(b => ({ ...b, created_by: req.user.user_id }));
    const count = await vacationModel.bulkUpsertBalances(items);
    res.json({ success: true, data: { count }, message: `${count}건 처리되었습니다` });
  } catch (err) { next(err); }
}

async function autoCalculate(req, res, next) {
  try {
    const { year, departmentId } = req.body;
    if (!year) return res.status(400).json({ success: false, error: '연도는 필수입니다' });
    const result = await vacationModel.autoCalculateForAllUsers(year, req.user.user_id, departmentId);
    res.json({ success: true, data: result, message: `${result.count}명 자동 배정 완료` });
  } catch (err) { next(err); }
}

/* ===== 장기근속 제외 설정 ===== */

async function setLongServiceExclusion(req, res, next) {
  try {
    const { user_id, excluded } = req.body;
    if (!user_id || excluded === undefined) {
      return res.status(400).json({ success: false, error: 'user_id와 excluded는 필수입니다' });
    }
    const { getPool } = require('../models/userModel');
    const db = getPool();
    const [result] = await db.query(
      'UPDATE sso_users SET long_service_excluded = ? WHERE user_id = ?',
      [excluded ? 1 : 0, user_id]
    );
    if (result.affectedRows === 0) {
      return res.status(404).json({ success: false, error: '해당 사용자를 찾을 수 없습니다' });
    }
    res.json({ success: true, message: `장기근속 제외 설정이 ${excluded ? '활성화' : '해제'}되었습니다` });
  } catch (err) { next(err); }
}

module.exports = {
  getVacationTypes, createVacationType, updateVacationType, deleteVacationType, updatePriorities,
  getBalancesByYear, getBalancesByUserYear, createBalance, updateBalance, deleteBalance,
  bulkUpsertBalances, autoCalculate, setLongServiceExclusion
};
