--[[
    SocietixM Plugin - Main Entry Point
    This is the main plugin script that loads all components
]]

local SocietixMPlugin = {}

-- Load all modules
local Server = require(script.Server)
local Network = require(script.Network)
local UI = require(script.UI)
local Core = require(script.Core)

-- Plugin state
local isInitialized = false
local server = nil

-- Initialize the plugin
function SocietixMPlugin:Initialize()
    if isInitialized then
        warn("[SocietixM] Plugin already initialized")
        return
    end
    
    print("[SocietixM] Initializing SocietixM Plugin...")
    
    -- Initialize UI
    UI:Initialize()
    
    -- Create server instance
    server = Server.new()
    server:Initialize()
    
    isInitialized = true
    print("[SocietixM] Plugin initialized successfully")
end

-- Create a new server
function SocietixMPlugin:CreateServer(config)
    if not isInitialized then
        self:Initialize()
    end
    
    if not server then
        warn("[SocietixM] Server not initialized")
        return false
    end
    
    return server:Create(config)
end

-- Start the server
function SocietixMPlugin:StartServer()
    if not server then
        warn("[SocietixM] Server not initialized")
        return false
    end
    
    return server:Start()
end

-- Stop the server
function SocietixMPlugin:StopServer()
    if not server then
        warn("[SocietixM] Server not initialized")
        return false
    end
    
    return server:Stop()
end

-- Get server status
function SocietixMPlugin:GetServerStatus()
    if not server then
        return {
            IsRunning = false,
            PlayerCount = 0,
            MaxPlayers = 0,
            ServerId = nil,
            SocietixId = nil
        }
    end
    
    return {
        IsRunning = server.IsRunning,
        PlayerCount = server:GetPlayerCount(),
        MaxPlayers = server:GetMaxPlayers(),
        ServerId = server.ServerId,
        SocietixId = server.SocietixId
    }
end

-- Get available servers
function SocietixMPlugin:GetAvailableServers(callback)
    if not isInitialized then
        self:Initialize()
    end
    
    Network:DiscoverServers(callback)
end

-- Connect to a server
function SocietixMPlugin:ConnectToServer(serverId, callback)
    if not isInitialized then
        self:Initialize()
    end
    
    Network:ConnectToServer(serverId, nil, callback)
end

-- Disconnect from server
function SocietixMPlugin:DisconnectFromServer(serverId)
    if not isInitialized then
        return
    end
    
    Network:DisconnectFromServer(serverId)
end

-- Generate Societix ID
function SocietixMPlugin:GenerateSocietixId(robloxGameId, callback)
    if not isInitialized then
        self:Initialize()
    end
    
    Network:GenerateSocietixId(robloxGameId, callback)
end

-- Resolve Societix ID
function SocietixMPlugin:ResolveSocietixId(societixId, callback)
    if not isInitialized then
        self:Initialize()
    end
    
    Network:ResolveSocietixId(societixId, callback)
end

-- Show notification
function SocietixMPlugin:ShowNotification(title, message, type)
    if not isInitialized then
        self:Initialize()
    end
    
    UI:ShowNotification(title, message, type)
end

-- Get plugin version
function SocietixMPlugin:GetVersion()
    return "1.0.0"
end

-- Cleanup
function SocietixMPlugin:Cleanup()
    if server then
        server:Shutdown()
        server = nil
    end
    
    UI:Cleanup()
    Network:Cleanup()
    
    isInitialized = false
    print("[SocietixM] Plugin cleaned up")
end

-- Auto-initialize when required
if script.Parent:IsA("Plugin") then
    -- This is running as a plugin
    SocietixMPlugin:Initialize()
    
    -- Cleanup when plugin is disabled
    script.Parent.Unloading:Connect(function()
        SocietixMPlugin:Cleanup()
    end)
end

return SocietixMPlugin
