-- | Core types for the libp2p Switch (central coordinator).
--
-- Defines connection states, direction, muxer session abstraction,
-- connection records, switch events, and the Switch itself.
--
module LibP2P.Switch.Types
  ( ConnState (..)
  , Direction (..)
  , MuxerSession (..)
  , Connection (..)
  , SwitchEvent (..)
  , StreamHandler
  , ConnectionGater (..)
  , defaultConnectionGater
  , Switch (..)
  , SwitchConfig (..)
  , defaultSwitchConfig
  , DialError (..)
  , BackoffEntry (..)
  , ResourceError (..)
  , ActiveListener (..)
  ) where

import Control.Concurrent.Async (Async)
import Control.Concurrent.STM (TChan, TMVar, TVar)
import Data.Map.Strict (Map)
import Data.Time.Clock (UTCTime)
import LibP2P.Crypto.Key (KeyPair)
import LibP2P.Switch.CertifiedRecords (CertifiedRecord)
import LibP2P.Crypto.PeerId (PeerId)
import LibP2P.Multiaddr (Multiaddr)
import LibP2P.MultistreamSelect.Negotiation (ProtocolId, StreamIO)
import LibP2P.Protocol.Identify.Message (IdentifyInfo)
import LibP2P.Switch.ResourceManager
  ( Direction (..)
  , MemoryAccount
  , ResourceError (..)
  , ResourceManager
  , ResourceScope
  )
import LibP2P.Transport (Listener, RelayCircuitLimit, Transport)
import LibP2P.Yamux.Types (YamuxConfig, defaultYamuxConfig)

-- | Connection state machine.
--
-- Connecting → ConnOpen → Closing → Closed
data ConnState
  = Connecting  -- ^ Raw transport established, upgrade in progress
  | ConnOpen    -- ^ Fully upgraded, streams can be opened/accepted
  | Closing     -- ^ Go Away sent/received, draining existing streams
  | ConnClosed  -- ^ Transport connection closed, resources freed
  deriving (Int -> ConnState -> ShowS
[ConnState] -> ShowS
ConnState -> String
(Int -> ConnState -> ShowS)
-> (ConnState -> String)
-> ([ConnState] -> ShowS)
-> Show ConnState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConnState -> ShowS
showsPrec :: Int -> ConnState -> ShowS
$cshow :: ConnState -> String
show :: ConnState -> String
$cshowList :: [ConnState] -> ShowS
showList :: [ConnState] -> ShowS
Show, ConnState -> ConnState -> Bool
(ConnState -> ConnState -> Bool)
-> (ConnState -> ConnState -> Bool) -> Eq ConnState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConnState -> ConnState -> Bool
== :: ConnState -> ConnState -> Bool
$c/= :: ConnState -> ConnState -> Bool
/= :: ConnState -> ConnState -> Bool
Eq)

-- | Abstract muxer session interface.
--
-- Decouples Switch from a specific muxer (Yamux, mplex, etc.).
-- Each muxer implementation provides a MuxerSession adapter.
data MuxerSession = MuxerSession
  { MuxerSession -> MemoryAccount -> IO StreamIO
muxOpenStream :: !(MemoryAccount -> IO StreamIO)
    -- ^ Open an outbound stream and close the account at terminal teardown
  , MuxerSession -> IO StreamIO
muxAcceptStream :: !(IO StreamIO) -- ^ Accept an already-accounted inbound stream
  , MuxerSession -> IO ()
muxClose :: !(IO ()) -- ^ Close the muxer session
  }

-- | An upgraded (secure + multiplexed) connection to a remote peer.
data Connection = Connection
  { Connection -> PeerId
connPeerId     :: !PeerId          -- ^ Remote peer identity
  , Connection -> Direction
connDirection  :: !Direction        -- ^ Inbound or outbound
  , Connection -> Multiaddr
connLocalAddr  :: !Multiaddr       -- ^ Local multiaddr
  , Connection -> Multiaddr
connRemoteAddr :: !Multiaddr       -- ^ Remote multiaddr
  , Connection -> ProtocolId
connSecurity   :: !ProtocolId      -- ^ Negotiated security protocol (e.g. "/noise" or "/tls/1.0.0")
  , Connection -> ProtocolId
connMuxer      :: !ProtocolId      -- ^ Negotiated muxer protocol (e.g. "/yamux/1.0.0")
  , Connection -> MuxerSession
connSession    :: !MuxerSession    -- ^ Muxer session for opening/accepting streams
  , Connection -> TVar ConnState
connState      :: !(TVar ConnState) -- ^ Mutable connection state
  , Connection -> ResourceScope
connResourceScope :: !ResourceScope -- ^ Resource scope owned by this connection
  , Connection -> Maybe RelayCircuitLimit
connRelayLimit :: !(Maybe RelayCircuitLimit)
    -- ^ Relay-advertised circuit budget. 'Nothing' unless this connection
    -- is the byte stream of a relayed circuit. A non-zero component makes
    -- 'LibP2P.Switch.Connection.newStream' refuse the connection unless
    -- the caller opts in.
  }

-- | A protocol stream handler.
--
-- Receives the connection the stream runs over and the stream I/O.
-- The connection exposes the remote peer identity ('connPeerId') and
-- addresses ('connRemoteAddr', 'connLocalAddr'), which protocols like
-- Identify (observedAddr) and the NAT stack need.
--
-- The handler owns the stream, matching go-libp2p: it must close or
-- reset on every exit path. Dispatch does not close on return, because
-- some handlers (Circuit Relay STOP) hand the stream off as a connection.
-- One-shot protocols (Identify, AutoNAT) close after the single exchange;
-- long-lived protocols (DHT, Ping, GossipSub) close when the remote
-- half-closes or the session ends.
type StreamHandler = Connection -> StreamIO -> IO ()

-- | Events emitted by the Switch for observability.
data SwitchEvent
  = Connected    !PeerId !Direction !Multiaddr  -- ^ Connection fully upgraded
  | Disconnected !PeerId !Direction !Multiaddr  -- ^ Connection closed
  deriving (Int -> SwitchEvent -> ShowS
[SwitchEvent] -> ShowS
SwitchEvent -> String
(Int -> SwitchEvent -> ShowS)
-> (SwitchEvent -> String)
-> ([SwitchEvent] -> ShowS)
-> Show SwitchEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SwitchEvent -> ShowS
showsPrec :: Int -> SwitchEvent -> ShowS
$cshow :: SwitchEvent -> String
show :: SwitchEvent -> String
$cshowList :: [SwitchEvent] -> ShowS
showList :: [SwitchEvent] -> ShowS
Show, SwitchEvent -> SwitchEvent -> Bool
(SwitchEvent -> SwitchEvent -> Bool)
-> (SwitchEvent -> SwitchEvent -> Bool) -> Eq SwitchEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SwitchEvent -> SwitchEvent -> Bool
== :: SwitchEvent -> SwitchEvent -> Bool
$c/= :: SwitchEvent -> SwitchEvent -> Bool
/= :: SwitchEvent -> SwitchEvent -> Bool
Eq)

-- | Policy hooks applied to inbound and outbound connection establishment.
data ConnectionGater = ConnectionGater
  { ConnectionGater -> Multiaddr -> IO Bool
gateAccept   :: !(Multiaddr -> IO Bool) -- ^ Check an inbound address before upgrade
  , ConnectionGater -> Multiaddr -> IO Bool
gateDialAddr :: !(Multiaddr -> IO Bool) -- ^ Check an outbound address before transport dial
  , ConnectionGater -> PeerId -> IO Bool
gateSecured  :: !(PeerId -> IO Bool)    -- ^ Check the authenticated remote peer
  }

-- | Default gater that allows all connections.
defaultConnectionGater :: ConnectionGater
defaultConnectionGater :: ConnectionGater
defaultConnectionGater = ConnectionGater
  { gateAccept :: Multiaddr -> IO Bool
gateAccept = \Multiaddr
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
  , gateDialAddr :: Multiaddr -> IO Bool
gateDialAddr = \Multiaddr
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
  , gateSecured :: PeerId -> IO Bool
gateSecured = \PeerId
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
  }

-- | Errors that can occur during a dial operation.
data DialError
  = DialBackoff               -- ^ Peer is in backoff period (recently failed)
  | DialNoAddresses           -- ^ No addresses provided for dialing
  | DialNoTransport !Multiaddr -- ^ No registered transport can handle this address
  | DialGatedAddress !Multiaddr -- ^ Outbound policy rejected this address
  | DialGatedPeer !PeerId     -- ^ Outbound policy rejected the authenticated peer
  | DialAllFailed ![String]   -- ^ All dial attempts failed
  | DialUpgradeFailed !String -- ^ Connection upgrade pipeline failed
  | DialSwitchClosed          -- ^ Switch has been shut down
  | DialResourceLimit !ResourceError  -- ^ Resource limit exceeded
  | DialPeerIdMismatch !PeerId !PeerId  -- ^ Expected vs actual remote PeerId
  deriving (Int -> DialError -> ShowS
[DialError] -> ShowS
DialError -> String
(Int -> DialError -> ShowS)
-> (DialError -> String)
-> ([DialError] -> ShowS)
-> Show DialError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialError -> ShowS
showsPrec :: Int -> DialError -> ShowS
$cshow :: DialError -> String
show :: DialError -> String
$cshowList :: [DialError] -> ShowS
showList :: [DialError] -> ShowS
Show, DialError -> DialError -> Bool
(DialError -> DialError -> Bool)
-> (DialError -> DialError -> Bool) -> Eq DialError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialError -> DialError -> Bool
== :: DialError -> DialError -> Bool
$c/= :: DialError -> DialError -> Bool
/= :: DialError -> DialError -> Bool
Eq)

-- | Per-peer dial backoff state.
--
-- After a failed dial, subsequent dials are rejected until the backoff
-- expires. Duration doubles on each consecutive failure up to 300s max.
data BackoffEntry = BackoffEntry
  { BackoffEntry -> UTCTime
beExpiry   :: !UTCTime  -- ^ When the backoff period ends
  , BackoffEntry -> Int
beAttempts :: !Int      -- ^ Number of consecutive failures (for exponential calc)
  } deriving (Int -> BackoffEntry -> ShowS
[BackoffEntry] -> ShowS
BackoffEntry -> String
(Int -> BackoffEntry -> ShowS)
-> (BackoffEntry -> String)
-> ([BackoffEntry] -> ShowS)
-> Show BackoffEntry
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BackoffEntry -> ShowS
showsPrec :: Int -> BackoffEntry -> ShowS
$cshow :: BackoffEntry -> String
show :: BackoffEntry -> String
$cshowList :: [BackoffEntry] -> ShowS
showList :: [BackoffEntry] -> ShowS
Show, BackoffEntry -> BackoffEntry -> Bool
(BackoffEntry -> BackoffEntry -> Bool)
-> (BackoffEntry -> BackoffEntry -> Bool) -> Eq BackoffEntry
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BackoffEntry -> BackoffEntry -> Bool
== :: BackoffEntry -> BackoffEntry -> Bool
$c/= :: BackoffEntry -> BackoffEntry -> Bool
/= :: BackoffEntry -> BackoffEntry -> Bool
Eq)

-- | An active listener bound to an address with its accept loop thread.
data ActiveListener = ActiveListener
  { ActiveListener -> Listener
alListener   :: !Listener      -- ^ The transport listener (accept, close)
  , ActiveListener -> Async ()
alAcceptLoop :: !(Async ())    -- ^ Background thread accepting connections
  , ActiveListener -> Multiaddr
alAddress    :: !Multiaddr     -- ^ Actual bound address (port 0 resolved)
  }

-- | Switch-wide settings, fixed when the Switch is created.
data SwitchConfig = SwitchConfig
  { SwitchConfig -> YamuxConfig
scYamuxConfig :: !YamuxConfig -- ^ Applied to every Yamux session the Switch upgrades (dial, listen, NAT probes)
  , SwitchConfig -> [ProtocolId]
scSecurityProtocols :: ![ProtocolId]
  -- ^ Secure channels offered, in preference order. The default is Noise,
  -- then TLS 1.3 ("/tls/1.0.0").
  }
  deriving (Int -> SwitchConfig -> ShowS
[SwitchConfig] -> ShowS
SwitchConfig -> String
(Int -> SwitchConfig -> ShowS)
-> (SwitchConfig -> String)
-> ([SwitchConfig] -> ShowS)
-> Show SwitchConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SwitchConfig -> ShowS
showsPrec :: Int -> SwitchConfig -> ShowS
$cshow :: SwitchConfig -> String
show :: SwitchConfig -> String
$cshowList :: [SwitchConfig] -> ShowS
showList :: [SwitchConfig] -> ShowS
Show, SwitchConfig -> SwitchConfig -> Bool
(SwitchConfig -> SwitchConfig -> Bool)
-> (SwitchConfig -> SwitchConfig -> Bool) -> Eq SwitchConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SwitchConfig -> SwitchConfig -> Bool
== :: SwitchConfig -> SwitchConfig -> Bool
$c/= :: SwitchConfig -> SwitchConfig -> Bool
/= :: SwitchConfig -> SwitchConfig -> Bool
Eq)

defaultSwitchConfig :: SwitchConfig
defaultSwitchConfig :: SwitchConfig
defaultSwitchConfig = SwitchConfig
  { scYamuxConfig :: YamuxConfig
scYamuxConfig = YamuxConfig
defaultYamuxConfig
  , scSecurityProtocols :: [ProtocolId]
scSecurityProtocols = [ProtocolId
"/noise", ProtocolId
"/tls/1.0.0"]
  }

-- | The Switch: central coordinator of the libp2p networking stack.
--
-- Manages transports, connection pool, protocol handlers, and events.
-- All mutable state is STM-based for safe concurrent access.
data Switch = Switch
  { Switch -> PeerId
swLocalPeerId  :: !PeerId                                            -- ^ This node's peer identity
  , Switch -> KeyPair
swIdentityKey  :: !KeyPair                                           -- ^ This node's key pair
  , Switch -> TVar [Transport]
swTransports   :: !(TVar [Transport])                                -- ^ Registered transports
  , Switch -> TVar (Map PeerId [Connection])
swConnPool     :: !(TVar (Map PeerId [Connection]))                  -- ^ Active connections per peer
  , Switch -> TVar (Map ProtocolId StreamHandler)
swProtocols    :: !(TVar (Map ProtocolId StreamHandler))             -- ^ Protocol registry
  , Switch -> TChan SwitchEvent
swEvents       :: !(TChan SwitchEvent)                               -- ^ Event broadcast channel
  , Switch -> TVar Bool
swClosed       :: !(TVar Bool)                                       -- ^ Whether the switch is shut down
  , Switch -> TVar (Map PeerId BackoffEntry)
swDialBackoffs :: !(TVar (Map PeerId BackoffEntry))                  -- ^ Per-peer dial backoff state
  , Switch -> TVar (Map PeerId (TMVar (Either DialError Connection)))
swPendingDials :: !(TVar (Map PeerId (TMVar (Either DialError Connection)))) -- ^ In-flight dials for dedup
  , Switch -> TVar ConnectionGater
swConnectionGater :: !(TVar ConnectionGater)                         -- ^ Connection admission policy
  , Switch -> ResourceManager
swResourceMgr  :: !ResourceManager                                   -- ^ Hierarchical resource manager
  , Switch -> TVar (Map PeerId IdentifyInfo)
swPeerStore    :: !(TVar (Map PeerId IdentifyInfo))                  -- ^ Identify info per peer
  , Switch -> TVar (Map PeerId CertifiedRecord)
swCertifiedRecords :: !(TVar (Map PeerId CertifiedRecord))           -- ^ Accepted RFC 0003 records per peer
  , Switch -> TVar [Connection -> IO ()]
swNotifiers    :: !(TVar [Connection -> IO ()])                      -- ^ Callbacks on new connection
  , Switch -> TVar [Connection -> IO ()]
swDisconnectNotifiers :: !(TVar [Connection -> IO ()])               -- ^ Callbacks on connection teardown
  , Switch -> TVar [ActiveListener]
swListeners    :: !(TVar [ActiveListener])                           -- ^ Active listeners
  , Switch -> TVar [IO ()]
swShutdownActions :: !(TVar [IO ()])                                 -- ^ Background service cleanup actions
  , Switch -> SwitchConfig
swConfig       :: !SwitchConfig                                      -- ^ Settings given at creation
  }