const dayLaborModel = require('../models/dayLaborModel');
const { getPool } = require('../models/partnerModel');
const notify = require('../shared/utils/notifyHelper');

// 일용직 요청 목록
async function list(req, res) {
  try {
    const { status, date_from, date_to, department_id, page, limit } = req.query;
    const rows = await dayLaborModel.findAll({
      status,
      date_from,
      date_to,
      department_id: department_id ? parseInt(department_id) : undefined,
      page: page ? parseInt(page) : 1,
      limit: limit ? parseInt(limit) : 50
    });
    res.json({ success: true, data: rows });
  } catch (err) {
    console.error('DayLabor list error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 일용직 요청 상세
async function getById(req, res) {
  try {
    const row = await dayLaborModel.findById(req.params.id);
    if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
    res.json({ success: true, data: row });
  } catch (err) {
    console.error('DayLabor get error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 일용직 요청 등록
async function create(req, res) {
  try {
    const { work_date, worker_count } = req.body;
    if (!work_date) {
      return res.status(400).json({ success: false, error: '작업일은 필수입니다' });
    }
    if (!worker_count || worker_count < 1) {
      return res.status(400).json({ success: false, error: '작업인원은 1명 이상이어야 합니다' });
    }
    const data = {
      ...req.body,
      requester_id: req.user.user_id || req.user.id
    };
    const row = await dayLaborModel.create(data);

    // 알림: 일용공 신청
    notify.send({
      type: 'day_labor',
      title: '일용공 신청',
      message: `${work_date} ${worker_count}명 일용공 신청`,
      link_url: '/daylabor.html',
      reference_type: 'day_labor_requests',
      reference_id: row.id || row.request_id,
      created_by: data.requester_id
    });

    res.status(201).json({ success: true, data: row });
  } catch (err) {
    console.error('DayLabor create error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 일용직 요청 승인
async function approve(req, res) {
  try {
    const id = req.params.id;
    const approvedBy = req.user.user_id || req.user.id;
    const row = await dayLaborModel.approve(id, approvedBy);
    if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });

    // 승인 시 안전교육 보고서 자동 생성
    if (row.status === 'approved') {
      try {
        const db = getPool();
        await db.query(
          `INSERT INTO safety_education_reports (target_type, target_id, education_date, status, registered_by)
           VALUES ('day_labor', ?, ?, 'planned', ?)`,
          [id, row.work_date, approvedBy]
        );
      } catch (safetyErr) {
        console.error('Safety report auto-create error:', safetyErr);
        // 안전교육 보고서 생성 실패해도 승인은 유지
      }
    }

    res.json({ success: true, data: row });
  } catch (err) {
    console.error('DayLabor approve error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 일용직 요청 거절
async function reject(req, res) {
  try {
    const id = req.params.id;
    const approvedBy = req.user.user_id || req.user.id;
    const { notes } = req.body;
    const row = await dayLaborModel.reject(id, approvedBy, notes);
    if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
    res.json({ success: true, data: row });
  } catch (err) {
    console.error('DayLabor reject error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 일용직 요청 완료
async function complete(req, res) {
  try {
    const row = await dayLaborModel.complete(req.params.id);
    if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
    res.json({ success: true, data: row });
  } catch (err) {
    console.error('DayLabor complete error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

// 통계
async function stats(req, res) {
  try {
    const { date_from, date_to } = req.query;
    const rows = await dayLaborModel.getStats({ date_from, date_to });
    res.json({ success: true, data: rows });
  } catch (err) {
    console.error('DayLabor stats error:', err);
    res.status(500).json({ success: false, error: err.message });
  }
}

module.exports = { list, getById, create, approve, reject, complete, stats };
