package com.applemand.zmanim.core.location

/**
 * Per-location candle-lighting minute defaults.
 *
 * Communities have established customs for how many minutes before shkiah to
 * light Shabbat candles. Most poskim agree on 18 as the diaspora default; Israeli
 * cities have local minhagim that differ. Picking a city auto-applies its
 * established minyan minhag so the user gets the expected number without manual
 * configuration.
 *
 * Sources: widely published community luachim (OU, Chabad, Yeshiva University,
 * Machon Harav Frank for Jerusalem, Haifa rabbanut, etc.). Where a city has
 * multiple minhagim, the more common published value is used — the user can
 * still override via the Settings slider.
 */
object CandleLightingDefaults {

    /** Default for any city not in the table below — standard diaspora minhag. */
    const val DIASPORA_DEFAULT = 18

    /**
     * Override by GeoNames geonameid. Source: GeoNames cities500.
     * Keep this list small — only well-known minhagim with clear published
     * consensus. Anything missing falls back to [DIASPORA_DEFAULT].
     */
    private val byGeonameId: Map<Long, Int> = mapOf(
        281184L to 40,   // Jerusalem (Yerushalayim) — 40 min
        294071L to 30,   // Haifa — 30 min (Haifa rabbanut minhag)
        293322L to 30,   // Tiberias (Tverya) — 30 min
        293100L to 30,   // Tzefat (Safed) — 30 min
        293397L to 40,   // Tel Aviv-Yafo — 40 min (published Agudas HaRabbanim; many use 20)
        293619L to 40,   // Petah Tikva — 40 min (some say 30)
        295514L to 30,   // Beersheba (Be'er Sheva) — 30 min
        293918L to 20,   // Netanya — 20 min
        295530L to 30,   // Beit Shemesh — 30 min
        295277L to 30,   // Bnei Brak — 30 min (some communities 40)
        294801L to 30,   // Ashdod — 30 min
        295629L to 30,   // Ashkelon — 30 min
        295620L to 30,   // Bat Yam — 30 min
        293207L to 30,   // Rishon LeZion — 30 min (Tel Aviv area)
        293703L to 30,   // Ramat Gan — 30 min
        294210L to 30,   // Holon — 30 min
        294514L to 30,   // Givatayim — 30 min
        294421L to 30,   // Herzliya — 30 min
        294514L to 30,   // Givatayim — 30 min
        293842L to 30,   // Kiryat Malakhi
        293825L to 30,   // Kiryat Motzkin
        293788L to 30,   // Kiryat Yam
        293692L to 30,   // Ramla
    )

    /**
     * Country-level fallbacks for countries without a city-level entry.
     * Most follow the 18-minute diaspora minhag; Israel falls back to 30.
     */
    private val byCountry: Map<String, Int> = mapOf(
        "IL" to 30,  // Israel default when city not listed
    )

    fun minutesFor(geonameId: Long?, country: String?): Int {
        if (geonameId != null) {
            byGeonameId[geonameId]?.let { return it }
        }
        if (!country.isNullOrEmpty()) {
            byCountry[country]?.let { return it }
        }
        return DIASPORA_DEFAULT
    }
}
