import type { City, District, Province } from '../types';

/**
 * Canned geography hierarchy for the client-side mock — a faithful subset of the b4 seed so the
 * cascade, the address book, and the coverage editor all demo before backend-phase-4 is
 * reachable. Ids mirror the contract where fixed (Tehran province 1 / city 101 / districts
 * 1001…1022) and are assigned deterministically for the other seeded capital cities. Every row
 * here is active; the mock never returns inactive regions (the real server filters them out).
 *
 * Shared by the geography mock (list endpoints) and the addresses/serviceAreas mocks (which
 * resolve a saved `cityId`/`districtId` back to its display names). Real data comes from the
 * server; this file is mock-only and imported nowhere in the production path.
 */

const FA_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
const toFaDigits = (n: number): string => String(n).replace(/\d/g, (d) => FA_DIGITS[Number(d)]);

// White-space second-tier cities (Mashhad, Isfahan, Shiraz, Tabriz, Ahvaz, Qom) the coverage
// model must serve, plus Tehran (the only seeded city with districts) and Karaj.
export const SEED_PROVINCES: Province[] = [
  { id: 1, nameFa: 'تهران', nameEn: 'Tehran', sortOrder: 0 },
  { id: 2, nameFa: 'خراسان رضوی', nameEn: 'Razavi Khorasan', sortOrder: 1 },
  { id: 3, nameFa: 'اصفهان', nameEn: 'Isfahan', sortOrder: 2 },
  { id: 4, nameFa: 'فارس', nameEn: 'Fars', sortOrder: 3 },
  { id: 5, nameFa: 'آذربایجان شرقی', nameEn: 'East Azerbaijan', sortOrder: 4 },
  { id: 6, nameFa: 'خوزستان', nameEn: 'Khuzestan', sortOrder: 5 },
  { id: 7, nameFa: 'قم', nameEn: 'Qom', sortOrder: 6 },
  { id: 8, nameFa: 'البرز', nameEn: 'Alborz', sortOrder: 7 },
];

export const SEED_CITIES: City[] = [
  { id: 101, provinceId: 1, nameFa: 'تهران', nameEn: 'Tehran', sortOrder: 0 },
  { id: 201, provinceId: 2, nameFa: 'مشهد', nameEn: 'Mashhad', sortOrder: 0 },
  { id: 301, provinceId: 3, nameFa: 'اصفهان', nameEn: 'Isfahan', sortOrder: 0 },
  { id: 401, provinceId: 4, nameFa: 'شیراز', nameEn: 'Shiraz', sortOrder: 0 },
  { id: 501, provinceId: 5, nameFa: 'تبریز', nameEn: 'Tabriz', sortOrder: 0 },
  { id: 601, provinceId: 6, nameFa: 'اهواز', nameEn: 'Ahvaz', sortOrder: 0 },
  { id: 701, provinceId: 7, nameFa: 'قم', nameEn: 'Qom', sortOrder: 0 },
  { id: 801, provinceId: 8, nameFa: 'کرج', nameEn: 'Karaj', sortOrder: 0 },
];

// Tehran's 22 مناطق (1001…1022); every other seeded city is whole-city-only (no districts).
export const SEED_DISTRICTS: District[] = Array.from({ length: 22 }, (_, index) => {
  const n = index + 1;
  return {
    id: 1000 + n,
    cityId: 101,
    nameFa: `منطقه ${toFaDigits(n)}`,
    nameEn: `District ${n}`,
    sortOrder: index,
  };
});

/** Resolve a saved city id back to its names + province (mock-only; for addresses/serviceAreas). */
export function resolveSeedCity(cityId: number): City | undefined {
  return SEED_CITIES.find((city) => city.id === cityId);
}

/** Resolve a saved district id back to its names (mock-only; null district = whole city). */
export function resolveSeedDistrict(districtId: number): District | undefined {
  return SEED_DISTRICTS.find((district) => district.id === districtId);
}
