import { getDb } from "../config/db.js";

export const verifyStaff = async (req, res) => {
  const db = getDb();
  const { code } = req.params;

  const [rows] = await db.execute(
    `SELECT u.name, u.role, n.name as notary_name, n.region, n.district
     FROM users u
     JOIN notaries n ON n.id = u.notary_id
     WHERE u.staff_id_code = ? AND u.is_active = 1`,
    [code]
  );

  if (rows.length === 0) {
    return res.status(404).json({ message: "Shaqaalahan lama aqoonsan. Fadlan hubi ID koodhka." });
  }

  res.json(rows[0]);
};
export const verifyNotary = async (req, res) => {
  const db = getDb();
  const { id } = req.params;

  const [rows] = await db.execute(
    `SELECT name, region, district, category, status, issued_date, expired_date 
     FROM notaries 
     WHERE id = ?`,
    [id]
  );

  if (rows.length === 0) {
    return res.status(404).json({ message: "Xafiiskan Nootaayada lama aqoonsan." });
  }

  res.json(rows[0]);
};

export const listNotaries = async (req, res) => {
  const db = getDb();
  const [rows] = await db.execute(
    "SELECT id, name, region, district, category, status FROM notaries WHERE status IN ('active', 'approved') ORDER BY name ASC"
  );
  res.json(rows);
};
