const { getPool } = require('../shared/config/database');

async function findToday() {
  const db = getPool();
  const [rows] = await db.query(
    `SELECT dv.*, pc.company_name AS partner_company_name
     FROM daily_visits dv
     LEFT JOIN partner_companies pc ON dv.company_id = pc.id
     WHERE dv.visit_date = CURDATE()
     ORDER BY dv.check_in_time DESC`
  );
  return rows;
}

async function getTodayStats() {
  const db = getPool();
  const [rows] = await db.query(
    `SELECT
       COUNT(*) AS total,
       SUM(CASE WHEN status = 'checked_in' THEN 1 ELSE 0 END) AS checked_in,
       SUM(CASE WHEN status IN ('checked_out','auto_checkout') THEN 1 ELSE 0 END) AS checked_out,
       SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
       SUM(visitor_count) AS total_visitors
     FROM daily_visits WHERE visit_date = CURDATE()`
  );
  return rows[0];
}

async function findAll({ visit_date, date_from, date_to, company_id, purpose, status, page = 1, limit = 50 } = {}) {
  const db = getPool();
  let sql = `SELECT dv.*, pc.company_name AS partner_company_name
             FROM daily_visits dv
             LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
  const params = [];
  if (visit_date) { sql += ' AND dv.visit_date = ?'; params.push(visit_date); }
  if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
  if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
  if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
  if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
  if (status) { sql += ' AND dv.status = ?'; params.push(status); }
  sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
  const offset = (page - 1) * limit;
  sql += ' LIMIT ? OFFSET ?';
  params.push(limit, offset);
  const [rows] = await db.query(sql, params);
  return rows;
}

async function findById(id) {
  const db = getPool();
  const [rows] = await db.query(
    `SELECT dv.*, pc.company_name AS partner_company_name
     FROM daily_visits dv
     LEFT JOIN partner_companies pc ON dv.company_id = pc.id
     WHERE dv.id = ?`,
    [id]
  );
  return rows[0] || null;
}

async function create(data) {
  const db = getPool();
  const [result] = await db.query(
    `INSERT INTO daily_visits (visit_date, company_id, company_name, visitor_name, visitor_count,
      purpose, purpose_detail, workplace_name, safety_education_yn, vehicle_number,
      check_in_time, notes, managing_department, registered_by)
     VALUES (CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?)`,
    [data.company_id || null, data.company_name || null, data.visitor_name, data.visitor_count || 1,
     data.purpose, data.purpose_detail || null, data.workplace_name || null,
     data.safety_education_yn || false, data.vehicle_number || null,
     data.notes || null, data.managing_department || null, data.registered_by]
  );
  // 개별 인원 명단 (선택)
  if (data.workers && data.workers.length > 0) {
    for (const w of data.workers) {
      await db.query(
        'INSERT INTO daily_visit_workers (daily_visit_id, partner_worker_id, worker_name) VALUES (?, ?, ?)',
        [result.insertId, w.partner_worker_id || null, w.worker_name]
      );
    }
  }
  return findById(result.insertId);
}

async function update(id, data) {
  const db = getPool();
  const fields = [];
  const values = [];
  if (data.company_id !== undefined) { fields.push('company_id = ?'); values.push(data.company_id || null); }
  if (data.company_name !== undefined) { fields.push('company_name = ?'); values.push(data.company_name || null); }
  if (data.visitor_name !== undefined) { fields.push('visitor_name = ?'); values.push(data.visitor_name); }
  if (data.visitor_count !== undefined) { fields.push('visitor_count = ?'); values.push(data.visitor_count); }
  if (data.purpose !== undefined) { fields.push('purpose = ?'); values.push(data.purpose); }
  if (data.purpose_detail !== undefined) { fields.push('purpose_detail = ?'); values.push(data.purpose_detail || null); }
  if (data.workplace_name !== undefined) { fields.push('workplace_name = ?'); values.push(data.workplace_name || null); }
  if (data.safety_education_yn !== undefined) { fields.push('safety_education_yn = ?'); values.push(data.safety_education_yn); }
  if (data.vehicle_number !== undefined) { fields.push('vehicle_number = ?'); values.push(data.vehicle_number || null); }
  if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
  if (data.managing_department !== undefined) { fields.push('managing_department = ?'); values.push(data.managing_department || null); }
  if (data.status !== undefined) { fields.push('status = ?'); values.push(data.status); }
  if (fields.length === 0) return findById(id);
  values.push(id);
  await db.query(`UPDATE daily_visits SET ${fields.join(', ')} WHERE id = ?`, values);
  return findById(id);
}

async function checkout(id, note) {
  const db = getPool();
  await db.query(
    `UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW(), checkout_note = ? WHERE id = ? AND status = 'checked_in'`,
    [note || null, id]
  );
  return findById(id);
}

async function bulkCheckout() {
  const db = getPool();
  const [result] = await db.query(
    `UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
  );
  return result;
}

async function autoCheckoutAll() {
  const db = getPool();
  const [result] = await db.query(
    `UPDATE daily_visits SET status = 'auto_checkout', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
  );
  return result;
}

async function deleteVisit(id) {
  const db = getPool();
  await db.query('DELETE FROM daily_visit_workers WHERE daily_visit_id = ?', [id]);
  await db.query('DELETE FROM daily_visits WHERE id = ?', [id]);
}

async function getStats({ date_from, date_to } = {}) {
  const db = getPool();
  const params = [];
  let dateFilter = '';
  if (date_from) { dateFilter += ' AND visit_date >= ?'; params.push(date_from); }
  if (date_to) { dateFilter += ' AND visit_date <= ?'; params.push(date_to); }

  const [byPurpose] = await db.query(
    `SELECT purpose, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY purpose ORDER BY cnt DESC`,
    params
  );
  const [byCompany] = await db.query(
    `SELECT COALESCE(pc.company_name, dv.company_name, '미등록') AS company, COUNT(*) AS cnt, SUM(dv.visitor_count) AS total_visitors
     FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1 ${dateFilter} GROUP BY company ORDER BY cnt DESC LIMIT 20`,
    params
  );
  const [byDate] = await db.query(
    `SELECT visit_date, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY visit_date ORDER BY visit_date DESC LIMIT 30`,
    params
  );
  return { byPurpose, byCompany, byDate };
}

async function exportCsv({ date_from, date_to, company_id, purpose } = {}) {
  const db = getPool();
  let sql = `SELECT dv.visit_date, COALESCE(pc.company_name, dv.company_name, '') AS company,
    dv.visitor_name, dv.visitor_count, dv.purpose, dv.purpose_detail, dv.workplace_name,
    dv.safety_education_yn, dv.vehicle_number, dv.check_in_time, dv.check_out_time,
    dv.status, dv.managing_department, dv.notes
    FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
  const params = [];
  if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
  if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
  if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
  if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
  sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
  const [rows] = await db.query(sql, params);
  return rows;
}

module.exports = {
  getPool, findToday, getTodayStats, findAll, findById, create, update,
  checkout, bulkCheckout, autoCheckoutAll, deleteVisit, getStats, exportCsv
};
