/********************************************************************************
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
*   Denisa Ros - initial API and implementation
********************************************************************************/
#ifdef _WIN32
#ifndef WIN_TUNNEL_CHANNEL_MGNT_H
#define WIN_TUNNEL_CHANNEL_MGNT_H

#include <support/component_impl.h>
#include <interfaces/ipc.h>
#include "connection.h"   
#include "watchdog.h"

#include <algorithm>
#include <map>
#include <mutex>
#include <string>

// Winsock headers are required for SOCKET / AF_UNIX / WSAStartup
#include <ws2tcpip.h>

class CWinTunnelConnection;

/**
 * @class CSocketsTunnelChannelMgnt
 * @brief IPC channel management class for Windows AF_UNIX tunnel communication.
 *
 * Similar to CSocketsChannelMgnt (proto=uds), but:
 *  - uses CWinTunnelConnection (tunnel wrapper) on top of CWinsockConnection
 *  - uses proto=tunnel in connect strings
 *
 * Provides creation and access to tunnel endpoints, manages server-side tunnel lifetimes,
 * and integrates with the SDV object/component framework.
 */
class CSocketsTunnelChannelMgnt :
    public sdv::CSdvObject,
    public sdv::ipc::ICreateEndpoint,
    public sdv::ipc::IChannelAccess
{
public:
    BEGIN_SDV_INTERFACE_MAP()
        SDV_INTERFACE_ENTRY(sdv::ipc::IChannelAccess)
        SDV_INTERFACE_ENTRY(sdv::ipc::ICreateEndpoint)
    END_SDV_INTERFACE_MAP()

    DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::system_object)
    DECLARE_OBJECT_CLASS_NAME("WinTunnelChannelControl")
    DECLARE_OBJECT_CLASS_ALIAS("unix_domain_sockets_tunnel")
    DECLARE_DEFAULT_OBJECT_NAME("unix_domain_sockets_tunnel")
    DECLARE_OBJECT_SINGLETON()

    virtual ~CSocketsTunnelChannelMgnt() = default;

    /**
     * @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
     * @return Returns 'true' when the initialization was successful, 'false' when not.
     */
    virtual bool OnInitialize() override;

    /**
     * @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
     */
    virtual void OnShutdown() override;

    /**
     * @brief Last function called before destruction. Overload of sdv::CSdvObject::OnDestroy.
     */
    virtual void OnDestroy() override;

    /**
     * @brief Creates a tunnel endpoint (server side) and returns endpoint info.
     * @param[in] cfgStr Optional config string (TOML or connect string).
     * @return The channel endpoint structure.
     */
    sdv::ipc::SChannelEndpoint CreateEndpoint(const sdv::u8string& cfgStr) override;

    /**
     * @brief Creates or accesses a connection object from the channel connect string.
     * @param[in] cs The channel connect string.
     * @return Pointer to connection access interface.
     */
    sdv::IInterfaceAccess* Access(const sdv::u8string& cs) override;

private:
    CWinTunnelConnectionWatchDog m_watchdog;
    std::map<const void*, std::shared_ptr<CWinTunnelConnection>> m_ptrConnections;  ///< Retain shared_ptr for all connections
    mutable std::mutex m_ConnectionsMutex;  ///< Protect concurrent access to m_ptrConnections
};

DEFINE_SDV_OBJECT(CSocketsTunnelChannelMgnt)

#endif // ! defined WIN_TUNNEL_CHANNEL_MGNT_H
#endif