const { getPool } = require('../middleware/auth');

// Get all safety checks
async function getAllChecks() {
  const db = getPool();
  const [rows] = await db.query(`
    SELECT c.check_id AS item_id, c.check_type AS item_type,
           c.check_item AS item_content, c.check_category AS category,
           c.display_order, c.is_active, c.is_required, c.description,
           c.weather_condition, wc.condition_name AS weather_condition_name,
           wc.condition_code AS weather_condition_id,
           c.task_id, t.task_name, t.work_type_id, wt.name AS work_type_name
    FROM tbm_safety_checks c
    LEFT JOIN tasks t ON c.task_id = t.task_id
    LEFT JOIN work_types wt ON t.work_type_id = wt.id
    LEFT JOIN weather_conditions wc ON c.weather_condition = wc.condition_code
    ORDER BY c.check_type, c.check_category, c.display_order, c.check_id
  `);
  return rows;
}

// Get check by ID
async function getCheckById(checkId) {
  const db = getPool();
  const [rows] = await db.query('SELECT * FROM tbm_safety_checks WHERE check_id = ?', [checkId]);
  return rows[0];
}

// Create check (frontend field names → DB column mapping)
async function createCheck(data) {
  const db = getPool();
  const check_type = data.item_type || data.check_type;
  const check_category = data.category || data.check_category || null;
  const check_item = data.item_content || data.check_item;
  const weather_condition = data.weather_condition_id || data.weather_condition || null;
  const { description, is_required, display_order, task_id } = data;
  const [result] = await db.query(
    'INSERT INTO tbm_safety_checks (check_type, check_category, check_item, description, is_required, display_order, weather_condition, task_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
    [check_type, check_category, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition, task_id || null]
  );
  return result.insertId;
}

// Update check (frontend field names → DB column mapping)
async function updateCheck(checkId, data) {
  const db = getPool();
  const check_type = data.item_type || data.check_type;
  const check_category = data.category || data.check_category || null;
  const check_item = data.item_content || data.check_item;
  const weather_condition = data.weather_condition_id || data.weather_condition || null;
  const { description, is_required, display_order, task_id } = data;
  const [result] = await db.query(
    'UPDATE tbm_safety_checks SET check_type = ?, check_category = ?, check_item = ?, description = ?, is_required = ?, display_order = ?, weather_condition = ?, task_id = ? WHERE check_id = ?',
    [check_type, check_category, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition, task_id || null, checkId]
  );
  return result;
}

// Delete check
async function deleteCheck(checkId) {
  const db = getPool();
  const [result] = await db.query('DELETE FROM tbm_safety_checks WHERE check_id = ?', [checkId]);
  return result;
}

// Get weather conditions
async function getWeatherConditions() {
  const db = getPool();
  const [rows] = await db.query('SELECT * FROM weather_conditions ORDER BY display_order, condition_code');
  return rows;
}

// Get work types
async function getWorkTypes() {
  const db = getPool();
  const [rows] = await db.query('SELECT id AS work_type_id, name AS work_type_name, category, description FROM work_types ORDER BY name');
  return rows;
}

// Get tasks by work type
async function getTasksByWorkType(workTypeId) {
  const db = getPool();
  const [rows] = await db.query('SELECT task_id, task_name, work_type_id, description, is_active FROM tasks WHERE work_type_id = ? AND is_active = TRUE ORDER BY task_name', [workTypeId]);
  return rows;
}

module.exports = { getAllChecks, getCheckById, createCheck, updateCheck, deleteCheck, getWeatherConditions, getWorkTypes, getTasksByWorkType };
