// controllers/workReportController.js
const workReportModel = require('../models/workReportModel');

// 1. CREATE: 단일 또는 다중 보고서 등록
exports.createWorkReport = async (req, res) => {
  try {
    const reports = Array.isArray(req.body) ? req.body : [req.body];
    const workReport_ids = [];

    for (const report of reports) {
      const id = await new Promise((resolve, reject) => {
        workReportModel.create(report, (err, insertId) => {
          if (err) reject(err);
          else resolve(insertId);
        });
      });
      workReport_ids.push(id);
    }

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

// 2. READ BY DATE
exports.getWorkReportsByDate = async (req, res) => {
  try {
    const { date } = req.params;
    const rows = await new Promise((resolve, reject) => {
      workReportModel.getAllByDate(date, (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
    });
    res.json(rows);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 3. READ BY RANGE
exports.getWorkReportsInRange = async (req, res) => {
  try {
    const { start, end } = req.query;
    const rows = await new Promise((resolve, reject) => {
      workReportModel.getByRange(start, end, (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
    });
    res.json(rows);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 4. READ ONE
exports.getWorkReportById = async (req, res) => {
  try {
    const { id } = req.params;
    const row = await new Promise((resolve, reject) => {
      workReportModel.getById(id, (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
    });
    if (!row) return res.status(404).json({ error: 'WorkReport not found' });
    res.json(row);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 5. UPDATE
exports.updateWorkReport = async (req, res) => {
  try {
    const { id } = req.params;
    const changes = await new Promise((resolve, reject) => {
      workReportModel.update(id, req.body, (err, affectedRows) => {
        if (err) reject(err);
        else resolve(affectedRows);
      });
    });
    if (changes === 0) return res.status(404).json({ error: 'No changes or not found' });
    res.json({ success: true, changes });
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 6. DELETE
exports.removeWorkReport = async (req, res) => {
  try {
    const { id } = req.params;
    const changes = await new Promise((resolve, reject) => {
      workReportModel.remove(id, (err, affectedRows) => {
        if (err) reject(err);
        else resolve(affectedRows);
      });
    });
    if (changes === 0) return res.status(404).json({ error: 'WorkReport not found' });
    res.json({ success: true, changes });
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};

// 7. SUMMARY (월간)
exports.getSummary = async (req, res) => {
  try {
    const { year, month } = req.query;
    if (!year || !month) {
      return res.status(400).json({ error: '연도와 월이 필요합니다 (year, month)' });
    }

    const start = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-01`;
    const end   = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-31`;

    const rows = await new Promise((resolve, reject) => {
      workReportModel.getByRange(start, end, (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
    });
    if (!rows || rows.length === 0) {
      return res.status(404).json({ error: 'WorkReport not found' });
    }

    res.json(rows);
  } catch (err) {
    res.status(500).json({ error: err.message || String(err) });
  }
};