--[[
    SocietixM Preloaded Map Model - Client Script
    This script runs on the client when the map is loaded
]]

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- Wait for SocietixM plugin to load
local SocietixMPlugin = ReplicatedStorage:WaitForChild("SocietixMPlugin", 10)
if not SocietixMPlugin then
    warn("[SocietixM] SocietixMPlugin not found in ReplicatedStorage")
    return
end

local UI = require(SocietixMPlugin.UI)
local Core = require(SocietixMPlugin.Core)

-- Initialize UI
UI:Initialize()

-- Create welcome message
local function createWelcomeMessage()
    local screenGui = Instance.new("ScreenGui")
    screenGui.Name = "SocietixMWelcome"
    screenGui.Parent = playerGui
    
    local frame = Instance.new("Frame")
    frame.Name = "WelcomeFrame"
    frame.Size = UDim2.new(0, 400, 0, 200)
    frame.Position = UDim2.new(0.5, -200, 0.5, -100)
    frame.BackgroundColor3 = Color3.new(0, 0, 0)
    frame.BackgroundTransparency = 0.3
    frame.BorderSizePixel = 0
    frame.Parent = screenGui
    
    -- Add rounded corners
    local corner = Instance.new("UICorner")
    corner.CornerRadius = UDim.new(0, 8)
    corner.Parent = frame
    
    -- Title
    local titleLabel = Instance.new("TextLabel")
    titleLabel.Name = "TitleLabel"
    titleLabel.Size = UDim2.new(1, -20, 0, 40)
    titleLabel.Position = UDim2.new(0, 10, 0, 10)
    titleLabel.BackgroundTransparency = 1
    titleLabel.Text = "Welcome to SocietixM!"
    titleLabel.TextColor3 = Color3.new(1, 1, 1)
    titleLabel.TextScaled = true
    titleLabel.Font = Enum.Font.GothamBold
    titleLabel.Parent = frame
    
    -- Message
    local messageLabel = Instance.new("TextLabel")
    messageLabel.Name = "MessageLabel"
    messageLabel.Size = UDim2.new(1, -20, 0, 100)
    messageLabel.Position = UDim2.new(0, 10, 0, 60)
    messageLabel.BackgroundTransparency = 1
    messageLabel.Text = "This is a custom server created with SocietixM.\n\nPress 'M' to open the SocietixM menu."
    messageLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)
    messageLabel.TextScaled = true
    messageLabel.Font = Enum.Font.Gotham
    messageLabel.TextXAlignment = Enum.TextXAlignment.Left
    messageLabel.TextYAlignment = Enum.TextYAlignment.Top
    messageLabel.Parent = frame
    
    -- Close button
    local closeButton = Instance.new("TextButton")
    closeButton.Name = "CloseButton"
    closeButton.Size = UDim2.new(0, 80, 0, 30)
    closeButton.Position = UDim2.new(1, -90, 1, -40)
    closeButton.BackgroundColor3 = Color3.new(0, 120, 215)
    closeButton.BorderSizePixel = 0
    closeButton.Text = "Got it!"
    closeButton.TextColor3 = Color3.new(1, 1, 1)
    closeButton.TextScaled = true
    closeButton.Font = Enum.Font.Gotham
    closeButton.Parent = frame
    
    -- Add rounded corners to button
    local buttonCorner = Instance.new("UICorner")
    buttonCorner.CornerRadius = UDim.new(0, 4)
    buttonCorner.Parent = closeButton
    
    -- Close button functionality
    closeButton.MouseButton1Click:Connect(function()
        local tween = TweenService:Create(frame, TweenInfo.new(0.3), {
            Size = UDim2.new(0, 0, 0, 0),
            Position = UDim2.new(0.5, 0, 0.5, 0)
        })
        tween:Play()
        
        tween.Completed:Connect(function()
            screenGui:Destroy()
        end)
    end)
    
    -- Auto-close after 10 seconds
    game:GetService("Debris"):AddItem(screenGui, 10)
end

-- Create key bindings
local function setupKeyBindings()
    local UserInputService = game:GetService("UserInputService")
    
    UserInputService.InputBegan:Connect(function(input, gameProcessed)
        if gameProcessed then return end
        
        if input.KeyCode == Enum.KeyCode.M then
            -- Toggle SocietixM menu
            local mainWindow = UI.activeWindows["MainWindow"]
            if mainWindow and mainWindow.Frame then
                mainWindow.Frame.Visible = not mainWindow.Frame.Visible
            end
        end
    end)
end

-- Create server info display
local function createServerInfoDisplay()
    local screenGui = Instance.new("ScreenGui")
    screenGui.Name = "SocietixMServerInfo"
    screenGui.Parent = playerGui
    
    local frame = Instance.new("Frame")
    frame.Name = "ServerInfoFrame"
    frame.Size = UDim2.new(0, 250, 0, 100)
    frame.Position = UDim2.new(0, 10, 0, 10)
    frame.BackgroundColor3 = Color3.new(0, 0, 0)
    frame.BackgroundTransparency = 0.3
    frame.BorderSizePixel = 0
    frame.Parent = screenGui
    
    -- Add rounded corners
    local corner = Instance.new("UICorner")
    corner.CornerRadius = UDim.new(0, 8)
    corner.Parent = frame
    
    -- Server name
    local nameLabel = Instance.new("TextLabel")
    nameLabel.Name = "ServerNameLabel"
    nameLabel.Size = UDim2.new(1, -10, 0, 25)
    nameLabel.Position = UDim2.new(0, 5, 0, 5)
    nameLabel.BackgroundTransparency = 1
    nameLabel.Text = "SocietixM Server"
    nameLabel.TextColor3 = Color3.new(1, 1, 1)
    nameLabel.TextScaled = true
    nameLabel.Font = Enum.Font.GothamBold
    nameLabel.TextXAlignment = Enum.TextXAlignment.Left
    nameLabel.Parent = frame
    
    -- Player count
    local playerLabel = Instance.new("TextLabel")
    playerLabel.Name = "PlayerCountLabel"
    playerLabel.Size = UDim2.new(1, -10, 0, 20)
    playerLabel.Position = UDim2.new(0, 5, 0, 30)
    playerLabel.BackgroundTransparency = 1
    playerLabel.Text = "Players: " .. #Players:GetPlayers() .. "/20"
    playerLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)
    playerLabel.TextScaled = true
    playerLabel.Font = Enum.Font.Gotham
    playerLabel.TextXAlignment = Enum.TextXAlignment.Left
    playerLabel.Parent = frame
    
    -- Server ID
    local idLabel = Instance.new("TextLabel")
    idLabel.Name = "ServerIdLabel"
    idLabel.Size = UDim2.new(1, -10, 0, 20)
    idLabel.Position = UDim2.new(0, 5, 0, 55)
    idLabel.BackgroundTransparency = 1
    idLabel.Text = "Server ID: Loading..."
    idLabel.TextColor3 = Color3.new(0.6, 0.6, 0.6)
    idLabel.TextScaled = true
    idLabel.Font = Enum.Font.Gotham
    idLabel.TextXAlignment = Enum.TextXAlignment.Left
    idLabel.Parent = frame
    
    -- Update player count
    spawn(function()
        while true do
            wait(1)
            if playerLabel and playerLabel.Parent then
                playerLabel.Text = "Players: " .. #Players:GetPlayers() .. "/20"
            end
        end
    end)
end

-- Initialize client
print("[SocietixM] Client initialized with map model")

-- Create welcome message
createWelcomeMessage()

-- Setup key bindings
setupKeyBindings()

-- Create server info display
createServerInfoDisplay()

-- Show notification
UI:ShowNotification("SocietixM", "Welcome to your custom server! Press 'M' to open the menu.", "success")
