import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"

const RAJAONGKIR_API_BASE = "https://rajaongkir.komerce.id/api/v1"

export async function GET(req: MedusaRequest, res: MedusaResponse) {
  const apiKey = process.env.RAJAONGKIR_API_KEY

  if (!apiKey) {
    return res.status(500).json({
      message: "RajaOngkir API key not configured",
    })
  }

  const { district_id } = req.params

  if (!district_id) {
    return res.status(400).json({
      message: "Parameter 'district_id' is required",
    })
  }

  const response = await fetch(
    `${RAJAONGKIR_API_BASE}/destination/sub-district/${district_id}`,
    {
      headers: { key: apiKey },
    }
  )

  const result = await response.json()
  res.json(result)
}
