import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it } from 'vitest'
import { DEFAULT_GUANGMING_VIEWPORT, useLocationStore, useMapStore } from '@/stores'

describe('map session store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('keeps category, selected POI and viewport while navigating back', () => {
    const store = useMapStore()
    store.setCategory('urban_park')
    store.selectPoi('poi_hongqiao_park')
    store.updateViewport({ longitude: 113.94, latitude: 22.76, scale: 13 })

    expect(store.categoryCode).toBe('urban_park')
    expect(store.selectedPoiId).toBe('poi_hongqiao_park')
    expect(store.viewport).toEqual({ longitude: 113.94, latitude: 22.76, scale: 13 })
  })

  it('shows and clears the planned-route overlay explicitly', () => {
    const store = useMapStore()
    expect(store.plannedRouteVisible).toBe(false)
    store.showPlannedRoute()
    expect(store.plannedRouteVisible).toBe(true)
    store.hidePlannedRoute()
    expect(store.plannedRouteVisible).toBe(false)
  })

  it('ignores invalid viewport updates', () => {
    const store = useMapStore()
    store.updateViewport({ longitude: Number.NaN, latitude: 22.76, scale: 13 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)
  })

  it('keeps a validated user location in memory independently from the map viewport', () => {
    const mapStore = useMapStore()
    const locationStore = useLocationStore()

    expect(locationStore.updateLocation(113.94, 22.76, 18)).toBe(true)
    expect(locationStore.snapshot).toMatchObject({
      longitude: 113.94,
      latitude: 22.76,
      accuracy: 18,
      coordinateSystem: 'GCJ02',
    })
    expect(locationStore.status).toBe('ready')
    expect(mapStore.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)
  })

  it('rejects malformed user coordinates', () => {
    const store = useLocationStore()
    expect(store.updateLocation(181, 22.76, 10)).toBe(false)
    expect(store.snapshot).toBeNull()
  })

  // 定位接口降级 / 地图 SDK 还没拿到定位时会回传 (0, 0)，
  // 它落在几内亚湾，会把地图甩到非洲外海并触发境外底图。
  it('rejects null-island coordinates for the map viewport', () => {
    const store = useMapStore()
    store.updateViewport({ longitude: 0, latitude: 0, scale: 15 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)

    store.updateViewport({ longitude: 113.94, latitude: 0, scale: 15 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)

    store.updateViewport({ longitude: 0, latitude: 22.76, scale: 15 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)
  })

  it('rejects null-island coordinates for the user location', () => {
    const store = useLocationStore()
    expect(store.updateLocation(0, 0, 10)).toBe(false)
    expect(store.updateLocation(113.94, 0, 10)).toBe(false)
    expect(store.updateLocation(0, 22.76, 10)).toBe(false)
    expect(store.snapshot).toBeNull()
    expect(store.status).toBe('idle')
  })

  it('falls back to the default viewport when reset is given an unusable one', () => {
    const store = useMapStore()
    store.updateViewport({ longitude: 113.94, latitude: 22.76, scale: 13 })

    store.resetViewport({ longitude: 0, latitude: 0, scale: 11 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)

    store.resetSession({ longitude: 0, latitude: 0, scale: 11 })
    expect(store.viewport).toEqual(DEFAULT_GUANGMING_VIEWPORT)
  })
})
