/** تنظیمات فروشگاه از دیتابیس (روش‌های ارسال و سقف ارسال رایگان) — در پنل مدیریت قابل ویرایش */
import "server-only";
import { cache } from "react";
import { db } from "@/lib/db";

const DEFAULT_FREE_SHIPPING = 2_000_000;

export const getShippingSettings = cache(async () => {
  const [methods, threshold] = await Promise.all([
    db.shippingMethod.findMany({ where: { isActive: true }, orderBy: { sortOrder: "asc" } }),
    db.setting.findUnique({ where: { key: "freeShippingThreshold" } }),
  ]);
  return {
    freeShippingThreshold: typeof threshold?.value === "number" ? threshold.value : DEFAULT_FREE_SHIPPING,
    methods,
  };
});
