// models/dailyWorkReportModel.js - 누적입력 방식 + 모든 기존 기능 포함
const { getDb } = require('../dbPool');

/**
 * 📋 마스터 데이터 조회 함수들
 */
const getAllWorkTypes = async (callback) => {
  try {
    const db = await getDb();
    const [rows] = await db.query('SELECT * FROM work_types ORDER BY name ASC');
    callback(null, rows);
  } catch (err) {
    console.error('작업 유형 조회 오류:', err);
    callback(err);
  }
};

const getAllWorkStatusTypes = async (callback) => {
  try {
    const db = await getDb();
    const [rows] = await db.query('SELECT * FROM work_status_types ORDER BY id ASC');
    callback(null, rows);
  } catch (err) {
    console.error('업무 상태 유형 조회 오류:', err);
    callback(err);
  }
};

const getAllErrorTypes = async (callback) => {
  try {
    const db = await getDb();
    const [rows] = await db.query('SELECT * FROM error_types ORDER BY name ASC');
    callback(null, rows);
  } catch (err) {
    console.error('에러 유형 조회 오류:', err);
    callback(err);
  }
};

/**
 * 🔄 누적 추가 전용 함수 (createDailyReport 대체) - 절대 삭제 안함!
 */
const createDailyReport = async (reportData, callback) => {
  const { report_date, worker_id, work_entries, created_by, created_by_name, total_hours } = reportData;
  const db = await getDb();
  const conn = await db.getConnection();

  try {
    await conn.beginTransaction();

    console.log(`📝 ${created_by_name}이 ${report_date} ${worker_id}번 작업자에게 데이터 추가 중...`);

   // ✅ 수정된 쿼리 (테이블 alias 추가):
const [existingReports] = await conn.query(
  `SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
   FROM daily_work_reports dwr
   LEFT JOIN Users u ON dwr.created_by = u.user_id
   WHERE dwr.report_date = ? AND dwr.worker_id = ?
   GROUP BY dwr.created_by`,
  [report_date, worker_id]
);


    console.log('기존 데이터 (삭제하지 않음):', existingReports);

    // 2. ✅ 삭제 없이 새로운 데이터만 추가!
    const insertedIds = [];
    for (const entry of work_entries) {
      const { project_id, work_type_id, work_status_id, error_type_id, work_hours } = entry;
      
      const [insertResult] = await conn.query(
        `INSERT INTO daily_work_reports 
         (report_date, worker_id, project_id, work_type_id, work_status_id, error_type_id, work_hours, created_by, created_at) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())`,
        [report_date, worker_id, project_id, work_type_id, work_status_id || 1, error_type_id || null, work_hours, created_by]
      );
      
      insertedIds.push(insertResult.insertId);
    }

   // ✅ 수정된 쿼리:
const [finalReports] = await conn.query(
  `SELECT dwr.created_by, u.name as created_by_name, COUNT(*) as count, SUM(dwr.work_hours) as total_hours
   FROM daily_work_reports dwr
   LEFT JOIN Users u ON dwr.created_by = u.user_id
   WHERE dwr.report_date = ? AND dwr.worker_id = ?
   GROUP BY dwr.created_by`,
  [report_date, worker_id]
);

    const grandTotal = finalReports.reduce((sum, report) => sum + parseFloat(report.total_hours || 0), 0);
    const myTotal = finalReports.find(r => r.created_by === created_by)?.total_hours || 0;

    console.log('최종 결과:');
    finalReports.forEach(report => {
      console.log(`  - ${report.created_by_name}: ${report.total_hours}시간 (${report.count}개 항목)`);
    });
    console.log(`  📊 총합: ${grandTotal}시간`);

    // 4. 감사 로그 추가
    try {
      await conn.query(
        `INSERT INTO work_report_audit_log 
         (action, report_id, new_values, changed_by, change_reason, created_at) 
         VALUES (?, ?, ?, ?, ?, NOW())`,
        [
          'ADD_ACCUMULATE', 
          insertedIds[0] || null,
          JSON.stringify({
            report_date,
            worker_id,
            work_entries_count: work_entries.length,
            added_hours: total_hours,
            my_total: myTotal,
            grand_total: grandTotal,
            contributors: finalReports.map(r => ({ name: r.created_by_name, hours: r.total_hours }))
          }),
          created_by,
          `누적 추가 by ${created_by_name} - 삭제 없음`
        ]
      );
    } catch (auditErr) {
      console.warn('감사 로그 추가 실패:', auditErr.message);
    }

    await conn.commit();
    
    callback(null, { 
      success: true, 
      inserted_count: insertedIds.length,
      deleted_count: 0, // 항상 0 (삭제 안함)
      action: 'accumulated',
      message: `${created_by_name}이 ${total_hours}시간 추가했습니다. (개인 총 ${myTotal}시간, 전체 총 ${grandTotal}시간)`,
      final_summary: {
        my_total: parseFloat(myTotal),
        grand_total: grandTotal,
        total_contributors: finalReports.length,
        contributors: finalReports
      }
    });
    
  } catch (err) {
    await conn.rollback();
    console.error('작업보고서 누적 추가 오류:', err);
    callback(err);
  } finally {
    conn.release();
  }
};

/**
 * 📊 특정 날짜 + 작업자 + 작성자의 누적 현황 조회
 */
const getMyAccumulatedHours = async (date, worker_id, created_by, callback) => {
  try {
    const db = await getDb();
    
    const sql = `
      SELECT 
        SUM(work_hours) as my_total_hours,
        COUNT(*) as my_entry_count,
        GROUP_CONCAT(
          CONCAT(p.project_name, ':', work_hours, 'h') 
          ORDER BY created_at
        ) as my_entries
      FROM daily_work_reports dwr
      LEFT JOIN Projects p ON dwr.project_id = p.project_id
      WHERE dwr.report_date = ? AND dwr.worker_id = ? AND dwr.created_by = ?
    `;
    
    const [rows] = await db.query(sql, [date, worker_id, created_by]);
    callback(null, rows[0] || { my_total_hours: 0, my_entry_count: 0, my_entries: null });
  } catch (err) {
    console.error('개인 누적 현황 조회 오류:', err);
    callback(err);
  }
};

/**
 * 📊 누적 현황 조회 - 날짜+작업자별 (모든 기여자)
 */
const getAccumulatedReportsByDate = async (date, worker_id, callback) => {
  try {
    const db = await getDb();
    
    const sql = getSelectQuery() + `
      WHERE dwr.report_date = ? AND dwr.worker_id = ?
      ORDER BY dwr.created_by, dwr.created_at ASC
    `;
    
    const [rows] = await db.query(sql, [date, worker_id]);
    callback(null, rows);
  } catch (err) {
    console.error('누적 현황 조회 오류:', err);
    callback(err);
  }
};

/**
 * 📊 기여자별 요약 조회
 */
const getContributorsByDate = async (date, worker_id, callback) => {
  try {
    const db = await getDb();
    
    const sql = `
      SELECT 
        dwr.created_by,
        u.name as created_by_name,
        COUNT(*) as entry_count,
        SUM(dwr.work_hours) as total_hours,
        MIN(dwr.created_at) as first_entry,
        MAX(dwr.created_at) as last_entry,
        GROUP_CONCAT(
          CONCAT(p.project_name, ':', dwr.work_hours, 'h') 
          ORDER BY dwr.created_at SEPARATOR ', '
        ) as entry_details
      FROM daily_work_reports dwr
      LEFT JOIN Users u ON dwr.created_by = u.user_id
      LEFT JOIN Projects p ON dwr.project_id = p.project_id
      WHERE dwr.report_date = ? AND dwr.worker_id = ?
      GROUP BY dwr.created_by
      ORDER BY total_hours DESC, first_entry ASC
    `;
    
    const [rows] = await db.query(sql, [date, worker_id]);
    callback(null, rows);
  } catch (err) {
    console.error('기여자별 요약 조회 오류:', err);
    callback(err);
  }
};

/**
 * 🗑️ 특정 작업 항목만 삭제 (개별 삭제)
 */
const removeSpecificEntry = async (entry_id, deleted_by, callback) => {
  const db = await getDb();
  const conn = await db.getConnection();
  
  try {
    await conn.beginTransaction();
    
    // 삭제 전 정보 확인
    const [entryInfo] = await conn.query(
      `SELECT dwr.*, w.worker_name, p.project_name, u.name as created_by_name
       FROM daily_work_reports dwr
       LEFT JOIN Workers w ON dwr.worker_id = w.worker_id
       LEFT JOIN Projects p ON dwr.project_id = p.project_id
       LEFT JOIN Users u ON dwr.created_by = u.user_id
       WHERE dwr.id = ?`,
      [entry_id]
    );

    if (entryInfo.length === 0) {
      await conn.rollback();
      return callback(new Error('삭제할 항목을 찾을 수 없습니다.'));
    }

    const entry = entryInfo[0];

    // 권한 확인: 본인이 작성한 것만 삭제 가능
    if (entry.created_by !== deleted_by) {
      await conn.rollback();
      return callback(new Error('본인이 작성한 항목만 삭제할 수 있습니다.'));
    }

    // 개별 항목 삭제
    const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [entry_id]);
    
    // 감사 로그
    try {
      await conn.query(
        `INSERT INTO work_report_audit_log 
         (action, report_id, old_values, changed_by, change_reason, created_at) 
         VALUES (?, ?, ?, ?, ?, NOW())`,
        [
          'DELETE_SINGLE', 
          entry_id,
          JSON.stringify({
            worker_name: entry.worker_name,
            project_name: entry.project_name,
            work_hours: entry.work_hours,
            report_date: entry.report_date
          }),
          deleted_by,
          `개별 항목 삭제`
        ]
      );
    } catch (auditErr) {
      console.warn('감사 로그 추가 실패:', auditErr.message);
    }

    await conn.commit();
    callback(null, { 
      success: true,
      deleted_entry: {
        worker_name: entry.worker_name,
        project_name: entry.project_name,
        work_hours: entry.work_hours
      }
    });
    
  } catch (err) {
    await conn.rollback();
    console.error('개별 항목 삭제 오류:', err);
    callback(err);
  } finally {
    conn.release();
  }
};

/**
 * 공통 SELECT 쿼리 부분
 */
const getSelectQuery = () => `
  SELECT 
    dwr.id,
    dwr.report_date,
    dwr.worker_id,
    dwr.project_id,
    dwr.work_type_id,
    dwr.work_status_id,
    dwr.error_type_id,
    dwr.work_hours,
    dwr.created_by,
    w.worker_name,
    p.project_name,
    wt.name as work_type_name,
    wst.name as work_status_name,
    et.name as error_type_name,
    u.name as created_by_name,
    dwr.created_at,
    dwr.updated_at
  FROM daily_work_reports dwr
  LEFT JOIN Workers w ON dwr.worker_id = w.worker_id
  LEFT JOIN Projects p ON dwr.project_id = p.project_id
  LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
  LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
  LEFT JOIN error_types et ON dwr.error_type_id = et.id
  LEFT JOIN Users u ON dwr.created_by = u.user_id
`;

/**
 * 7. ID로 작업보고서 조회
 */
const getById = async (id, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + 'WHERE dwr.id = ?';
    const [rows] = await db.query(sql, [id]);
    callback(null, rows[0] || null);
  } catch (err) {
    console.error('ID로 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 8. 일일 작업보고서 조회 (날짜별)
 */
const getByDate = async (date, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + `
      WHERE dwr.report_date = ?
      ORDER BY w.worker_name ASC, p.project_name ASC, dwr.id ASC
    `;
    const [rows] = await db.query(sql, [date]);
    callback(null, rows);
  } catch (err) {
    console.error('날짜별 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 9. 일일 작업보고서 조회 (날짜 + 작성자별)
 */
const getByDateAndCreator = async (date, created_by, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + `
      WHERE dwr.report_date = ? AND dwr.created_by = ?
      ORDER BY w.worker_name ASC, p.project_name ASC, dwr.id ASC
    `;
    const [rows] = await db.query(sql, [date, created_by]);
    callback(null, rows);
  } catch (err) {
    console.error('날짜+작성자별 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 10. 일일 작업보고서 조회 (작업자별)
 */
const getByWorker = async (worker_id, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + `
      WHERE dwr.worker_id = ?
      ORDER BY dwr.report_date DESC, dwr.id ASC
    `;
    const [rows] = await db.query(sql, [worker_id]);
    callback(null, rows);
  } catch (err) {
    console.error('작업자별 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 11. 일일 작업보고서 조회 (날짜 + 작업자)
 */
const getByDateAndWorker = async (date, worker_id, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + `
      WHERE dwr.report_date = ? AND dwr.worker_id = ?
      ORDER BY dwr.id ASC
    `;
    const [rows] = await db.query(sql, [date, worker_id]);
    callback(null, rows);
  } catch (err) {
    console.error('날짜+작업자별 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 12. 기간별 조회
 */
const getByRange = async (start_date, end_date, callback) => {
  try {
    const db = await getDb();
    const sql = getSelectQuery() + `
      WHERE dwr.report_date BETWEEN ? AND ?
      ORDER BY dwr.report_date DESC, w.worker_name ASC, dwr.id ASC
    `;
    const [rows] = await db.query(sql, [start_date, end_date]);
    callback(null, rows);
  } catch (err) {
    console.error('기간별 작업보고서 조회 오류:', err);
    callback(err);
  }
};

/**
 * 13. 상세 검색 (페이지네이션 포함)
 */
const searchWithDetails = async (params, callback) => {
  const { start_date, end_date, worker_id, project_id, work_status_id, created_by, page, limit } = params;
  
  try {
    const db = await getDb();
    
    // 조건 구성
    let whereConditions = ['dwr.report_date BETWEEN ? AND ?'];
    let queryParams = [start_date, end_date];

    if (worker_id) {
      whereConditions.push('dwr.worker_id = ?');
      queryParams.push(worker_id);
    }

    if (project_id) {
      whereConditions.push('dwr.project_id = ?');
      queryParams.push(project_id);
    }

    if (work_status_id) {
      whereConditions.push('dwr.work_status_id = ?');
      queryParams.push(work_status_id);
    }

    if (created_by) {
      whereConditions.push('dwr.created_by = ?');
      queryParams.push(created_by);
    }

    const whereClause = whereConditions.join(' AND ');
    
    // 총 개수 조회
    const countQuery = `
      SELECT COUNT(*) as total 
      FROM daily_work_reports dwr 
      WHERE ${whereClause}
    `;
    const [countResult] = await db.query(countQuery, queryParams);
    const total = countResult[0].total;

    // 데이터 조회 (JOIN 포함)
    const offset = (page - 1) * limit;
    const dataQuery = getSelectQuery() + `
      WHERE ${whereClause}
      ORDER BY dwr.report_date DESC, w.worker_name ASC, dwr.created_at DESC
      LIMIT ? OFFSET ?
    `;
    
    const dataParams = [...queryParams, limit, offset];
    const [rows] = await db.query(dataQuery, dataParams);

    callback(null, { reports: rows, total });
  } catch (err) {
    console.error('상세 검색 오류:', err);
    callback(err);
  }
};

/**
 * 14. 일일 근무 요약 조회 (날짜별)
 */
const getSummaryByDate = async (date, callback) => {
  try {
    const db = await getDb();
    
    const sql = `
      SELECT 
        dwr.worker_id,
        w.worker_name,
        dwr.report_date,
        SUM(dwr.work_hours) as total_hours,
        COUNT(*) as work_entries_count,
        SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
      FROM daily_work_reports dwr
      LEFT JOIN Workers w ON dwr.worker_id = w.worker_id
      WHERE dwr.report_date = ?
      GROUP BY dwr.worker_id, dwr.report_date
      ORDER BY w.worker_name ASC
    `;
    const [rows] = await db.query(sql, [date]);
    callback(null, rows);
  } catch (err) {
    console.error('일일 근무 요약 조회 오류:', err);
    callback(err);
  }
};

/**
 * 15. 일일 근무 요약 조회 (작업자별)
 */
const getSummaryByWorker = async (worker_id, callback) => {
  try {
    const db = await getDb();
    
    const sql = `
      SELECT 
        dwr.report_date,
        dwr.worker_id,
        w.worker_name,
        SUM(dwr.work_hours) as total_hours,
        COUNT(*) as work_entries_count,
        SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count
      FROM daily_work_reports dwr
      LEFT JOIN Workers w ON dwr.worker_id = w.worker_id
      WHERE dwr.worker_id = ?
      GROUP BY dwr.report_date, dwr.worker_id
      ORDER BY dwr.report_date DESC
    `;
    const [rows] = await db.query(sql, [worker_id]);
    callback(null, rows);
  } catch (err) {
    console.error('작업자별 근무 요약 조회 오류:', err);
    callback(err);
  }
};

/**
 * 16. 월간 요약
 */
const getMonthlySummary = async (year, month, callback) => {
  try {
    const db = await getDb();
    const start = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-01`;
    const end = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-31`;
    
    const sql = `
      SELECT 
        dwr.report_date,
        dwr.worker_id,
        w.worker_name,
        SUM(dwr.work_hours) as total_work_hours,
        COUNT(DISTINCT dwr.project_id) as project_count,
        COUNT(*) as work_entries_count,
        SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as error_count,
        GROUP_CONCAT(DISTINCT p.project_name ORDER BY p.project_name) as projects,
        GROUP_CONCAT(DISTINCT wt.name ORDER BY wt.name) as work_types
      FROM daily_work_reports dwr
      LEFT JOIN Workers w ON dwr.worker_id = w.worker_id
      LEFT JOIN Projects p ON dwr.project_id = p.project_id
      LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
      WHERE dwr.report_date BETWEEN ? AND ?
      GROUP BY dwr.report_date, dwr.worker_id
      ORDER BY dwr.report_date DESC, w.worker_name ASC
    `;
    const [rows] = await db.query(sql, [start, end]);
    callback(null, rows);
  } catch (err) {
    console.error('월간 요약 조회 오류:', err);
    callback(err);
  }
};

/**
 * 17. 작업보고서 수정
 */
const updateById = async (id, updateData, callback) => {
  try {
    const db = await getDb();
    
    const setFields = [];
    const values = [];
    
    if (updateData.work_hours !== undefined) {
      setFields.push('work_hours = ?');
      values.push(updateData.work_hours);
    }
    
    if (updateData.work_status_id !== undefined) {
      setFields.push('work_status_id = ?');
      values.push(updateData.work_status_id);
    }
    
    if (updateData.error_type_id !== undefined) {
      setFields.push('error_type_id = ?');
      values.push(updateData.error_type_id);
    }
    
    if (updateData.project_id !== undefined) {
      setFields.push('project_id = ?');
      values.push(updateData.project_id);
    }
    
    if (updateData.work_type_id !== undefined) {
      setFields.push('work_type_id = ?');
      values.push(updateData.work_type_id);
    }
    
    setFields.push('updated_at = NOW()');
    
    if (updateData.updated_by) {
      setFields.push('updated_by = ?');
      values.push(updateData.updated_by);
    }
    
    values.push(id);
    
    const sql = `UPDATE daily_work_reports SET ${setFields.join(', ')} WHERE id = ?`;
    const [result] = await db.query(sql, values);
    
    callback(null, result.affectedRows);
  } catch (err) {
    console.error('작업보고서 수정 오류:', err);
    callback(err);
  }
};

/**
 * 18. 특정 작업보고서 삭제
 */
const removeById = async (id, deletedBy, callback) => {
  const db = await getDb();
  const conn = await db.getConnection();
  
  try {
    await conn.beginTransaction();
    
    // 삭제 전 정보 저장 (감사 로그용)
    const [reportInfo] = await conn.query('SELECT * FROM daily_work_reports WHERE id = ?', [id]);
    
    // 작업보고서 삭제
    const [result] = await conn.query('DELETE FROM daily_work_reports WHERE id = ?', [id]);
    
    // 감사 로그 추가
    if (reportInfo.length > 0 && deletedBy) {
      try {
        await conn.query(
          `INSERT INTO work_report_audit_log 
           (action, report_id, old_values, changed_by, change_reason, created_at) 
           VALUES ('DELETE', ?, ?, ?, 'Manual deletion', NOW())`,
          [id, JSON.stringify(reportInfo[0]), deletedBy]
        );
      } catch (auditErr) {
        console.warn('감사 로그 추가 실패:', auditErr.message);
      }
    }
    
    await conn.commit();
    callback(null, result.affectedRows);
  } catch (err) {
    await conn.rollback();
    console.error('작업보고서 삭제 오류:', err);
    callback(err);
  } finally {
    conn.release();
  }
};

/**
 * 19. 작업자의 특정 날짜 전체 삭제
 */
const removeByDateAndWorker = async (date, worker_id, deletedBy, callback) => {
  const db = await getDb();
  const conn = await db.getConnection();
  
  try {
    await conn.beginTransaction();
    
    // 삭제 전 정보 저장 (감사 로그용)
    const [reportInfos] = await conn.query(
      'SELECT * FROM daily_work_reports WHERE report_date = ? AND worker_id = ?', 
      [date, worker_id]
    );
    
    // 작업보고서 삭제
    const [result] = await conn.query(
      'DELETE FROM daily_work_reports WHERE report_date = ? AND worker_id = ?',
      [date, worker_id]
    );
    
    // 감사 로그 추가
    if (reportInfos.length > 0 && deletedBy) {
      try {
        await conn.query(
          `INSERT INTO work_report_audit_log 
           (action, old_values, changed_by, change_reason, created_at) 
           VALUES ('DELETE_BATCH', ?, ?, 'Batch deletion by date and worker', NOW())`,
          [JSON.stringify({ deleted_reports: reportInfos, count: reportInfos.length }), deletedBy]
        );
      } catch (auditErr) {
        console.warn('감사 로그 추가 실패:', auditErr.message);
      }
    }
    
    await conn.commit();
    callback(null, result.affectedRows);
  } catch (err) {
    await conn.rollback();
    console.error('작업보고서 전체 삭제 오류:', err);
    callback(err);
  } finally {
    conn.release();
  }
};

/**
 * 20. 통계 조회
 */
const getStatistics = async (start_date, end_date, callback) => {
  try {
    const db = await getDb();
    
    const sql = `
      SELECT 
        COUNT(*) as total_reports,
        SUM(work_hours) as total_hours,
        COUNT(DISTINCT worker_id) as unique_workers,
        COUNT(DISTINCT project_id) as unique_projects,
        SUM(CASE WHEN work_status_id = 2 THEN 1 ELSE 0 END) as error_count,
        AVG(work_hours) as avg_hours_per_entry,
        MIN(work_hours) as min_hours,
        MAX(work_hours) as max_hours
      FROM daily_work_reports
      WHERE report_date BETWEEN ? AND ?
    `;
    
    const [rows] = await db.query(sql, [start_date, end_date]);
    
    // 추가 통계 - 날짜별 집계
    const dailyStatsSql = `
      SELECT 
        report_date,
        COUNT(*) as daily_reports,
        SUM(work_hours) as daily_hours,
        COUNT(DISTINCT worker_id) as daily_workers
      FROM daily_work_reports
      WHERE report_date BETWEEN ? AND ?
      GROUP BY report_date
      ORDER BY report_date DESC
    `;
    
    const [dailyStats] = await db.query(dailyStatsSql, [start_date, end_date]);
    
    const result = {
      overall: rows[0],
      daily_breakdown: dailyStats
    };
    
    callback(null, result);
  } catch (err) {
    console.error('통계 조회 오류:', err);
    callback(err);
  }
};

// 모든 함수 내보내기 (기존 기능 + 누적 기능)
module.exports = {
  // 📋 마스터 데이터
  getAllWorkTypes,
  getAllWorkStatusTypes,
  getAllErrorTypes,
  
  // 🔄 핵심 생성 함수 (누적 방식)
  createDailyReport,              // 누적 추가 (덮어쓰기 없음)
  
  // 📊 누적 관련 새로운 함수들
  getMyAccumulatedHours,          // 개인 누적 현황
  getAccumulatedReportsByDate,    // 날짜별 누적 현황
  getContributorsByDate,          // 기여자별 요약
  removeSpecificEntry,            // 개별 항목 삭제
  
  // 📊 기존 조회 함수들 (모두 유지)
  getById,
  getByDate,
  getByDateAndCreator,           // 날짜+작성자별 조회
  getByWorker,
  getByDateAndWorker,
  getByRange,
  searchWithDetails,
  getSummaryByDate,
  getSummaryByWorker,
  getMonthlySummary,
  
  // ✏️ 수정/삭제 함수들 (기존 유지)
  updateById,
  removeById,
  removeByDateAndWorker,
  getStatistics
};