import { create } from 'zustand';
import type {
  PatrolShip,
  PatrolRoute,
  PatrolScenario,
  CoverageZone,
} from '@data/mock/patrols';
import { getPatrolShips, toLegacyPatrolShip } from '@/services/patrol';

interface PatrolStore {
  ships: PatrolShip[];
  routes: Record<string, PatrolRoute>;
  scenarios: PatrolScenario[];
  coverage: CoverageZone[];
  fleetRoutes: Record<string, [number, number][]>;
  selectedShipId: string | null;
  loaded: boolean;
  loading: boolean;
  error: string | null;
  load: () => Promise<void>;
  selectShip: (id: string | null) => void;
}

export const usePatrolStore = create<PatrolStore>((set, get) => ({
  ships: [],
  routes: {},
  scenarios: [],
  coverage: [],
  fleetRoutes: {},
  selectedShipId: null,
  loaded: false,
  loading: false,
  error: null,

  load: async () => {
    if (get().loaded && !get().error) return;

    set({ loading: true, error: null });
    try {
      // 함정 목록은 API에서, 나머지(routes/scenarios/coverage)는 mock 유지
      const [apiShips, mockModule] = await Promise.all([
        getPatrolShips(),
        get().routes && Object.keys(get().routes).length > 0
          ? Promise.resolve(null)
          : import('@data/mock/patrols').then((m) => ({
              routes: m.MOCK_PATROL_ROUTES,
              scenarios: m.MOCK_PATROL_SCENARIOS,
              coverage: m.MOCK_COVERAGE_ZONES,
              fleetRoutes: m.MOCK_FLEET_ROUTES,
            })),
      ]);

      set({
        ships: apiShips.map(toLegacyPatrolShip),
        routes: mockModule?.routes ?? get().routes,
        scenarios: mockModule?.scenarios ?? get().scenarios,
        coverage: mockModule?.coverage ?? get().coverage,
        fleetRoutes: mockModule?.fleetRoutes ?? get().fleetRoutes,
        loaded: true,
        loading: false,
      });
    } catch (err) {
      set({ error: err instanceof Error ? err.message : String(err), loading: false });
    }
  },

  selectShip: (id) => set({ selectedShipId: id }),
}));
