import { apiBase } from './config'
import { useAuthStore } from '../store/authStore'

function authHeaders() {
  const token = useAuthStore.getState().token
  return token ? { Authorization: `Bearer ${token}` } : {}
}

export async function fetchPackageQuote({
  packageName,
  checkin,
  checkout,
  roomCode,
  adult,
  kid,
  elderly,
  nationality,
}) {
  const params = new URLSearchParams({
    packageName: packageName || '',
    checkin: checkin || '',
    checkout: checkout || '',
    roomCode: roomCode || '',
    adult: String(adult ?? 1),
    kid: String(kid ?? 0),
    elderly: String(elderly ?? 0),
    nationality: nationality || 'Malaysian',
  })

  const res = await fetch(`${apiBase}/api/pricing/quote?${params.toString()}`, {
    method: 'GET',
    headers: authHeaders(),
  })

  if (!res.ok) {
    const msg = await res.text().catch(() => '')
    throw new Error(msg || `Failed to get package price (${res.status})`)
  }

  return res.json()
}
