const { getDb } = require('../dbPool');

// 1. 문서 업로드
const create = async (doc) => {
  const db = await getDb();
  const sql = `
    INSERT INTO uploaded_documents
      (title, tags, description, original_name, stored_name, file_path, file_type, file_size, submitted_by)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  `;
  const values = [
    doc.title,
    doc.tags,
    doc.description,
    doc.original_name,
    doc.stored_name,
    doc.file_path,
    doc.file_type,
    doc.file_size,
    doc.submitted_by
  ];
  const [result] = await db.query(sql, values);
  return result.insertId;
};

// 2. 전체 문서 목록 조회
const getAll = async () => {
  const db = await getDb();
  const [rows] = await db.query(`SELECT * FROM uploaded_documents ORDER BY created_at DESC`);
  return rows;
};

module.exports = {
  create,
  getAll
};
