import { createContext, useContext, useState, useCallback } from 'react'

const SpotifyContext = createContext()

export function SpotifyProvider({ children }) {
  // Account management
  const [accounts, setAccounts] = useState(() => {
    const saved = localStorage.getItem('spotify_accounts')
    return saved ? JSON.parse(saved) : []
  })

  // Zone to account mapping: { zoneId: accountId }
  const [zoneAccounts, setZoneAccounts] = useState(() => {
    const saved = localStorage.getItem('zone_accounts')
    return saved ? JSON.parse(saved) : {}
  })

  // Active Spotify Connect instances per account
  const [spotifyConnects, setSpotifyConnects] = useState({})

  // Add or update account
  const addAccount = useCallback((account) => {
    setAccounts(prev => {
      const filtered = prev.filter(a => a.id !== account.id)
      const updated = [...filtered, account]
      localStorage.setItem('spotify_accounts', JSON.stringify(updated))
      return updated
    })
  }, [])

  // Remove account
  const removeAccount = useCallback((accountId) => {
    setAccounts(prev => {
      const updated = prev.filter(a => a.id !== accountId)
      localStorage.setItem('spotify_accounts', JSON.stringify(updated))
      return updated
    })
    
    // Remove zone mappings for this account
    setZoneAccounts(prev => {
      const updated = { ...prev }
      Object.keys(updated).forEach(zoneId => {
        if (updated[zoneId] === accountId) delete updated[zoneId]
      })
      localStorage.setItem('zone_accounts', JSON.stringify(updated))
      return updated
    })
  }, [])

  // Assign account to zone
  const assignAccountToZone = useCallback((zoneId, accountId) => {
    setZoneAccounts(prev => {
      const updated = { ...prev, [zoneId]: accountId }
      localStorage.setItem('zone_accounts', JSON.stringify(updated))
      return updated
    })

    // Activate Spotify Connect for this account if not already active
    if (accountId && !spotifyConnects[accountId]) {
      activateSpotifyConnect(accountId)
    }
  }, [spotifyConnects])

  // Remove account from zone
  const removeAccountFromZone = useCallback((zoneId) => {
    const accountId = zoneAccounts[zoneId]
    setZoneAccounts(prev => {
      const updated = { ...prev }
      delete updated[zoneId]
      localStorage.setItem('zone_accounts', JSON.stringify(updated))
      return updated
    })

    // Deactivate Spotify Connect if no zones use this account
    if (accountId && !Object.values(zoneAccounts).includes(accountId)) {
      deactivateSpotifyConnect(accountId)
    }
  }, [zoneAccounts])

  // Activate Spotify Connect for account
  const activateSpotifyConnect = useCallback((accountId) => {
    if (!accountId) return
    
    setSpotifyConnects(prev => {
      if (prev[accountId]) return prev
      
      return {
        ...prev,
        [accountId]: {
          id: `spotify-${accountId}`,
          status: 'active',
          device: 'Bordanlage',
          connectedAt: new Date().toISOString(),
        }
      }
    })
  }, [])

  // Deactivate Spotify Connect for account
  const deactivateSpotifyConnect = useCallback((accountId) => {
    setSpotifyConnects(prev => {
      const updated = { ...prev }
      delete updated[accountId]
      return updated
    })
  }, [])

  // Get account for zone
  const getAccountForZone = useCallback((zoneId) => {
    const accountId = zoneAccounts[zoneId]
    if (!accountId) return null
    return accounts.find(a => a.id === accountId)
  }, [zoneAccounts, accounts])

  // Get zones using account
  const getZonesForAccount = useCallback((accountId) => {
    return Object.entries(zoneAccounts)
      .filter(([_, aId]) => aId === accountId)
      .map(([zId]) => zId)
  }, [zoneAccounts])

  return (
    <SpotifyContext.Provider
      value={{
        accounts,
        addAccount,
        removeAccount,
        zoneAccounts,
        assignAccountToZone,
        removeAccountFromZone,
        spotifyConnects,
        activateSpotifyConnect,
        deactivateSpotifyConnect,
        getAccountForZone,
        getZonesForAccount,
      }}
    >
      {children}
    </SpotifyContext.Provider>
  )
}

export function useSpotify() {
  const context = useContext(SpotifyContext)
  if (!context) {
    throw new Error('useSpotify must be used within SpotifyProvider')
  }
  return context
}
