import { q as query } from '../../../../chunks/db_Dsd4uyc_.mjs';
export { renderers } from '../../../../renderers.mjs';

const PUT = async ({ params, request }) => {
  try {
    const id = parseInt(params.id);
    const body = await request.json();
    const fields = [];
    const values = [];
    let idx = 1;
    const allowed = ["name", "merchant_name", "feed_url", "sync_frequency", "is_enabled", "priority", "config", "external_id"];
    for (const key of allowed) {
      if (body[key] !== void 0) {
        fields.push(`${key} = $${idx}`);
        values.push(key === "config" ? JSON.stringify(body[key]) : body[key]);
        idx++;
      }
    }
    if (fields.length === 0) {
      return new Response(JSON.stringify({ error: "No fields to update" }), { status: 400 });
    }
    fields.push(`updated_at = NOW()`);
    values.push(id);
    const result = await query(
      `UPDATE feeds SET ${fields.join(", ")} WHERE id = $${idx} RETURNING *`,
      values
    );
    if (result.rowCount === 0) {
      return new Response(JSON.stringify({ error: "Feed not found" }), { status: 404 });
    }
    return new Response(JSON.stringify(result.rows[0]), {
      status: 200,
      headers: { "Content-Type": "application/json" }
    });
  } catch (err) {
    return new Response(JSON.stringify({ error: err.message }), { status: 500 });
  }
};
const DELETE = async ({ params }) => {
  try {
    const id = parseInt(params.id);
    await query("DELETE FROM product_offers WHERE feed_id = $1", [id]);
    await query("DELETE FROM sync_logs WHERE feed_id = $1", [id]);
    await query("DELETE FROM field_mappings WHERE feed_id = $1", [id]);
    const result = await query("DELETE FROM feeds WHERE id = $1 RETURNING id", [id]);
    if (result.rowCount === 0) {
      return new Response(JSON.stringify({ error: "Feed not found" }), { status: 404 });
    }
    return new Response(JSON.stringify({ deleted: true }), {
      status: 200,
      headers: { "Content-Type": "application/json" }
    });
  } catch (err) {
    return new Response(JSON.stringify({ error: err.message }), { status: 500 });
  }
};

const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
  __proto__: null,
  DELETE,
  PUT
}, Symbol.toStringTag, { value: 'Module' }));

const page = () => _page;

export { page };
