import type { AppSettings } from '@/shared/contracts/appSettings'
import {
  isLocalLaunchMode,
  normalizeAPIBaseURL,
  trimTrailingSlash,
} from '@movscript/core/shared'

const DEFAULT_API_ORIGIN = 'http://localhost:8765'
const LOCAL_API_ORIGIN = 'http://localhost:8766'
export const APP_SETTINGS_STORAGE_KEY = 'movscript-app-settings'

export type { AppSettings }
export { isLocalLaunchMode, normalizeAPIBaseURL, trimTrailingSlash }

function readStoredAPIBaseURL(): string | null {
  if (typeof window === 'undefined') return null
  try {
    const raw = window.localStorage.getItem(APP_SETTINGS_STORAGE_KEY)
    if (!raw) return null
    const parsed = JSON.parse(raw) as Partial<AppSettings> & {
      state?: { settings?: Partial<AppSettings> }
      settings?: Partial<AppSettings>
    }
    const settings = parsed.state?.settings ?? parsed.settings ?? parsed
    return typeof settings.apiBaseURL === 'string' && settings.apiBaseURL.trim()
      ? normalizeAPIBaseURL(settings.apiBaseURL)
      : null
  } catch {
    return null
  }
}

export function getDefaultAPIBaseURL(): string {
  if (typeof window !== 'undefined' && window.location.pathname.startsWith('/admin')) {
    return normalizeAPIBaseURL(window.location.origin)
  }
  return normalizeAPIBaseURL(readImportMetaEnv().VITE_API_BASE_URL || DEFAULT_API_ORIGIN)
}

export function getLocalAPIBaseURL(): string {
  return normalizeAPIBaseURL(readImportMetaEnv().VITE_LOCAL_API_BASE_URL || LOCAL_API_ORIGIN)
}

export function getAPIBaseURL(): string {
  return readStoredAPIBaseURL() || getDefaultAPIBaseURL()
}

export function getAPIV1BaseURL(): string {
  return `${getAPIBaseURL()}/api/v1`
}

export const API_BASE_URL = getAPIBaseURL()
export const API_V1_BASE_URL = getAPIV1BaseURL()

function readImportMetaEnv(): Record<string, string | undefined> {
  return (import.meta as { env?: Record<string, string | undefined> }).env ?? {}
}
