const processModel = require('../models/processModel');
const projectModel = require('../models/projectModel');

// 1. 공정 등록
exports.createProcess = async (req, res) => {
  try {
    const processData = req.body;

    if (!processData.process_end) {
      const project = await new Promise((resolve, reject) => {
        projectModel.getById(processData.project_id, (err, row) => {
          if (err) return reject(err);
          if (!row) return reject({ status: 404, message: 'Project not found' });
          resolve(row);
        });
      });

      processData.process_end = project.due_date;
    }

    const lastID = await new Promise((resolve, reject) => {
      processModel.create(processData, (err, id) => (err ? reject(err) : resolve(id)));
    });

    res.json({ success: true, process_id: lastID });
  } catch (err) {
    const status = err.status || 500;
    res.status(status).json({ error: err.message || String(err) });
  }
};

// 2. 전체 조회
exports.getAllProcesses = async (req, res) => {
  try {
    const rows = await new Promise((resolve, reject) => {
      processModel.getAll((err, data) => (err ? reject(err) : resolve(data)));
    });
    res.json(rows);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 3. 단일 조회
exports.getProcessById = async (req, res) => {
  try {
    const id = parseInt(req.params.process_id, 10);
    const row = await new Promise((resolve, reject) => {
      processModel.getById(id, (err, data) => (err ? reject(err) : resolve(data)));
    });
    if (!row) return res.status(404).json({ error: 'Process not found' });
    res.json(row);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 4. 수정
exports.updateProcess = async (req, res) => {
  try {
    const id = parseInt(req.params.process_id, 10);
    const processData = { ...req.body, process_id: id };

    if (!processData.process_end) {
      const project = await new Promise((resolve, reject) => {
        projectModel.getById(processData.project_id, (err, row) => {
          if (err) return reject(err);
          if (!row) return reject({ status: 404, message: 'Project not found' });
          resolve(row);
        });
      });

      processData.process_end = project.due_date;
    }

    const changes = await new Promise((resolve, reject) => {
      processModel.update(processData, (err, ch) => (err ? reject(err) : resolve(ch)));
    });

    if (changes === 0) return res.status(404).json({ error: 'No changes or not found' });
    res.json({ success: true, changes });
  } catch (err) {
    const status = err.status || 500;
    res.status(status).json({ error: err.message || String(err) });
  }
};

// 5. 삭제
exports.removeProcess = async (req, res) => {
  try {
    const id = parseInt(req.params.process_id, 10);
    const changes = await new Promise((resolve, reject) => {
      processModel.remove(id, (err, ch) => (err ? reject(err) : resolve(ch)));
    });
    if (changes === 0) return res.status(404).json({ error: 'Process not found' });
    res.json({ success: true, changes });
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};