import { q as query } from './db_Du3AmLCY.mjs';

async function getBlogPosts(options = {}) {
  const { limit = 12, offset = 0, category, published = true } = options;
  const conditions = [];
  const params = [];
  let i = 1;
  if (published !== void 0) {
    conditions.push(`is_published = $${i++}`);
    params.push(published);
  }
  if (category) {
    conditions.push(`category = $${i++}`);
    params.push(category);
  }
  const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
  const countResult = await query(`SELECT COUNT(*)::int as total FROM blog_posts ${where}`, params);
  const total = countResult.rows[0]?.total || 0;
  const result = await query(
    `SELECT * FROM blog_posts ${where} ORDER BY published_at DESC NULLS LAST LIMIT $${i++} OFFSET $${i++}`,
    [...params, limit, offset]
  );
  return { posts: result.rows, total };
}
async function getBlogPostBySlug(slug) {
  const result = await query(
    "SELECT * FROM blog_posts WHERE slug = $1 AND is_published = true LIMIT 1",
    [slug]
  );
  return result.rows[0] || null;
}
async function getBlogCategories() {
  const result = await query(
    `SELECT category, COUNT(*)::int as count FROM blog_posts WHERE is_published = true GROUP BY category ORDER BY count DESC`
  );
  return result.rows;
}
async function getRelatedPosts(category, excludeSlug, limit = 3) {
  const result = await query(
    `SELECT * FROM blog_posts WHERE is_published = true AND category = $1 AND slug != $2 ORDER BY published_at DESC LIMIT $3`,
    [category, excludeSlug, limit]
  );
  return result.rows;
}

export { getRelatedPosts as a, getBlogPosts as b, getBlogCategories as c, getBlogPostBySlug as g };
