import http from "k6/http";
import { check, sleep } from "k6";

const BASE_URL = __ENV.BASE_URL || "http://localhost:3000";
const CUSTOMER_ID = __ENV.CUSTOMER_ID || "CUST_001";

export const options = {
  vus: 1,
  duration: "70s",
  thresholds: {
    http_req_failed: ["rate<0.02"],
  },
};

const headers = {
  "Content-Type": "application/json",
  "x-auth-type": "service",
  "x-customer-id": CUSTOMER_ID,
  "x-permissions": "pricing:markup:write",
};

const body = JSON.stringify({
  request_id: "00000000-0000-4000-8000-000000000001",
  customer_id: CUSTOMER_ID,
  pickup_address: {
    street: "1234 Warehouse Blvd",
    city: "Los Angeles",
    state: "CA",
    zip: "90001",
    place_id: "ChIJ_pickup",
    formatted_address: "1234 Warehouse Blvd, Los Angeles, CA 90001, USA",
    selected_from_suggestions: true,
  },
  delivery_address: {
    street: "5678 Distribution Dr",
    city: "Dallas",
    state: "TX",
    zip: "75201",
    place_id: "ChIJ_delivery",
    formatted_address: "5678 Distribution Dr, Dallas, TX 75201, USA",
    selected_from_suggestions: true,
  },
  weight: { value: 500, unit: "lb" },
  dimensions: { length: 48, width: 40, height: 48, unit: "in" },
  pallet_count: 2,
  cargo_type: "general_freight",
});

/** TC-303：customer 60 次/分钟限流 */
export default function rateLimitTest() {
  const res = http.post(`${BASE_URL}/api/quotes`, body, { headers });
  check(res, {
    "status is 200 or 429": (r) => r.status === 200 || r.status === 429,
  });
  sleep(0.5);
}
