/**
 * Address-based pricing — FRONTEND STUB.
 *
 * Designed to mirror the eventual hub contract so the UI can be built now
 * and the backend swapped in later without reshaping callers.
 *
 * Final shape (planned):
 *   PricingArea  (table): id, name, default_distance_included_m,
 *                         fee_per_extra_meter, currency, notes
 *   Address      (added fields): pricing_area_id (fk nullable),
 *                                distance_to_fiber_m,
 *                                qualification_status,
 *                                extras[] (memo surcharges)
 *   AddressExtra (table): id, address_id, label, category, amount, note,
 *                         created_by, created_at
 *
 * For MVP we only *compute* distance-based fees; extras are stored verbatim
 * but not automated — they persist per address as memo surcharges that the
 * operator typed in during a previous survey or quote.
 *
 * To swap to real data, replace mockAddressPricing() with a hub fetch.
 */

const DEFAULT_INCLUDED_M = 20
const DEFAULT_RATE_PER_M = 5.00

export const PRICING_EXTRA_CATEGORIES = [
  { value: 'distance',  label: 'Distance', icon: 'straighten' },
  { value: 'forest',    label: 'Émondage / forêt', icon: 'forest' },
  { value: 'civil',     label: 'Génie civil', icon: 'engineering' },
  { value: 'pole',      label: 'Poteau / location', icon: 'flag' },
  { value: 'mdu',       label: 'Immeuble (MDU)', icon: 'apartment' },
  { value: 'other',     label: 'Autre', icon: 'build' },
]

const MOCK_PRICING_AREAS = {
  'PA-URBAN': { id: 'PA-URBAN', name: 'Zone urbaine standard', default_distance_included_m: 20, fee_per_extra_meter: 5.00, currency: 'CAD' },
  'PA-RURAL': { id: 'PA-RURAL', name: 'Zone rurale', default_distance_included_m: 30, fee_per_extra_meter: 8.00, currency: 'CAD' },
  'PA-REMOTE': { id: 'PA-REMOTE', name: 'Zone éloignée', default_distance_included_m: 50, fee_per_extra_meter: 12.00, currency: 'CAD' },
}

const MOCK_ADDRESSES = {
  'addr-standard': {
    address_id: 'addr-standard',
    label: 'Urbain standard (inclus, 0$ extras)',
    pricing_area: MOCK_PRICING_AREAS['PA-URBAN'],
    distance_to_fiber_m: 15,
    extras: [],
    qualification_status: 'auto_qualified',
  },
  'addr-urban-extra': {
    address_id: 'addr-urban-extra',
    label: 'Urbain avec extension 35m',
    pricing_area: MOCK_PRICING_AREAS['PA-URBAN'],
    distance_to_fiber_m: 55,
    extras: [],
    qualification_status: 'auto_qualified',
  },
  'addr-rural': {
    address_id: 'addr-rural',
    label: 'Rural avec émondage (survey)',
    pricing_area: MOCK_PRICING_AREAS['PA-RURAL'],
    distance_to_fiber_m: 85,
    extras: [
      { id: 'ex_rural_forest', label: 'Émondage 3 arbres', category: 'forest', amount: 150, note: 'Accès chemin forestier' },
    ],
    qualification_status: 'surveyed',
  },
  'addr-remote-complex': {
    address_id: 'addr-remote-complex',
    label: 'Éloigné, complexe (non qualifié)',
    pricing_area: MOCK_PRICING_AREAS['PA-REMOTE'],
    distance_to_fiber_m: 220,
    extras: [
      { id: 'ex_rem_pole', label: 'Location poteau Hydro', category: 'pole', amount: 75, note: 'Bail annuel' },
      { id: 'ex_rem_trench', label: 'Traversée de route', category: 'civil', amount: 450, note: 'Permis municipal requis' },
    ],
    qualification_status: 'unqualified',
  },
  'addr-no-area': {
    address_id: 'addr-no-area',
    label: 'Sans zone tarifaire (fallback défaut)',
    pricing_area: null,
    distance_to_fiber_m: 40,
    extras: [],
    qualification_status: 'auto_qualified',
  },
}

export const MOCK_ADDRESS_OPTIONS = Object.values(MOCK_ADDRESSES).map(a => ({
  value: a.address_id,
  label: a.label,
}))

export function defaultPricingArea () {
  return {
    id: null,
    name: 'Tarif par défaut',
    default_distance_included_m: DEFAULT_INCLUDED_M,
    fee_per_extra_meter: DEFAULT_RATE_PER_M,
    currency: 'CAD',
  }
}

// Stub — will be replaced with GET /api/pricing/address/:id
// For unknown address ids (real Service Location names), we return a
// sensible default profile so the wizard can open meaningfully. The
// operator can still override via the test-scenario dropdown.
export async function mockAddressPricing (addressId) {
  await new Promise(r => setTimeout(r, 120))
  const entry = MOCK_ADDRESSES[addressId]
  if (entry) {
    return { ...entry, extras: entry.extras.map(e => ({ ...e })) }
  }
  return {
    address_id: addressId,
    label: `Adresse ${addressId} (profil par défaut — stub)`,
    pricing_area: null,
    distance_to_fiber_m: 0,
    extras: [],
    qualification_status: 'auto_qualified',
  }
}
