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

async function getCategoryTree(dimension) {
  const params = [];
  let where = "WHERE c.is_active = TRUE";
  if (dimension) {
    where += ` AND c.dimension = $1`;
    params.push(dimension);
  }
  const { rows } = await query(
    `SELECT * FROM categories c ${where} ORDER BY c.depth, c.sort_order, c.name_en`,
    params
  );
  const byId = /* @__PURE__ */ new Map();
  const roots = [];
  for (const row of rows) {
    row.children = [];
    byId.set(row.id, row);
  }
  for (const row of rows) {
    if (row.parent_id && byId.has(row.parent_id)) {
      byId.get(row.parent_id).children.push(row);
    } else {
      roots.push(row);
    }
  }
  return roots;
}
async function getCategoryById(id) {
  const { rows } = await query(
    "SELECT * FROM categories WHERE id = $1",
    [id]
  );
  return rows[0] || null;
}
async function createCategory(data) {
  let depth = 0;
  if (data.parent_id) {
    const parent = await getCategoryById(data.parent_id);
    if (parent) depth = parent.depth + 1;
  }
  const { rows } = await query(
    `INSERT INTO categories (slug, name_en, name_nl, dimension, parent_id, depth, sort_order, icon_url)
     VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
     RETURNING *`,
    [data.slug, data.name_en, data.name_nl, data.dimension, data.parent_id || null, depth, data.sort_order || 0, data.icon_url || null]
  );
  return rows[0];
}
async function updateCategory(id, data) {
  const fields = [];
  const params = [];
  let idx = 1;
  for (const [key, value] of Object.entries(data)) {
    if (value !== void 0) {
      fields.push(`${key} = $${idx}`);
      params.push(value);
      idx++;
    }
  }
  if (fields.length === 0) return getCategoryById(id);
  fields.push(`updated_at = now()`);
  params.push(id);
  const { rows } = await query(
    `UPDATE categories SET ${fields.join(", ")} WHERE id = $${idx} RETURNING *`,
    params
  );
  return rows[0] || null;
}
async function deleteCategory(id) {
  const { rowCount } = await query(
    "UPDATE categories SET is_active = FALSE, updated_at = now() WHERE id = $1",
    [id]
  );
  return (rowCount ?? 0) > 0;
}
async function getProductsByCategory(slug, options = {}) {
  const page = options.page || 1;
  const limit = Math.min(options.limit || 24, 100);
  const offset = (page - 1) * limit;
  const sortMap = {
    "price_asc": "p.price ASC",
    "price_desc": "p.price DESC",
    "newest": "p.created_at DESC",
    "name": "p.name ASC",
    "discount": `CASE WHEN p.original_price > 0 THEN (p.original_price - p.price) / p.original_price ELSE 0 END DESC`
  };
  const orderBy = sortMap[options.sort || "newest"] || "p.created_at DESC";
  const { rows: [countRow] } = await query(
    `SELECT COUNT(*) as total FROM product_categories pc
     JOIN categories c ON c.id = pc.category_id
     WHERE (c.slug = $1 OR c.slug LIKE $1 || '/%') AND c.is_active = TRUE`,
    [slug]
  );
  const { rows: products } = await query(
    `SELECT DISTINCT p.* FROM products p
     JOIN product_categories pc ON pc.product_id = p.id
     JOIN categories c ON c.id = pc.category_id
     WHERE (c.slug = $1 OR c.slug LIKE $1 || '/%') AND c.is_active = TRUE
     ORDER BY ${orderBy}
     LIMIT $2 OFFSET $3`,
    [slug, limit, offset]
  );
  return { products, total: parseInt(countRow.total) };
}

export { getProductsByCategory as a, createCategory as c, deleteCategory as d, getCategoryTree as g, updateCategory as u };
