import { useState } from 'react'
import {
Box, Flex, Heading, Text, Button, Input, Select, Textarea,
SimpleGrid, Card, Tabs, Table, VStack, createListCollection,
} from '@chakra-ui/react'
import { FiAlertTriangle } from 'react-icons/fi'
import client from '../../api/client'
import { useApp } from '../../stores/AppContext'
const SCOPE_OPTIONS = ['all', 'server', 'partition']
const SERVER_OPTIONS = ['na', 'eu', 'sea', 'sa', 'oc']
const PARTITION_OPTIONS = ['parrish', 'sietch', 'bazar']
const serverCollection = createListCollection({
items: SERVER_OPTIONS.map((s) => ({ value: s, label: s.toUpperCase() })),
})
const partitionCollection = createListCollection({
items: PARTITION_OPTIONS.map((p) => ({ value: p, label: p })),
})
const itemFieldCollection = createListCollection({
items: [
{ value: 'stack_size', label: 'stack_size' },
{ value: 'quality_level', label: 'quality_level' },
{ value: 'durability', label: 'durability' },
{ value: 'ammo', label: 'ammo' },
],
})
const demoStateCollection = createListCollection({
items: [
{ value: 'true', label: 'Demo' },
{ value: 'false', label: 'Normal' },
],
})
const limitCollection = createListCollection({
items: [10, 25, 50, 100, 250].map((n) => ({ value: String(n), label: `${n} entries` })),
})
export default function AdminTools() {
const [tab, setTab] = useState<'broadcast' | 'economy' | 'world' | 'character' | 'functions' | 'raw' | 'audit'>('broadcast')
const { dispatch } = useApp()
return (
Admin Tools
setTab(e.value as any)} mb={6}>
{([
{ key: 'broadcast', label: 'Broadcast' },
{ key: 'economy', label: 'Economy' },
{ key: 'world', label: 'World' },
{ key: 'character', label: 'Character' },
{ key: 'functions', label: 'Functions' },
{ key: 'raw', label: 'Raw Query' },
{ key: 'audit', label: 'Audit Log' },
] as const).map((t) => (
{t.label}
))}
)
}
function BroadcastTab({ dispatch }: { dispatch: any }) {
const [message, setMessage] = useState('')
const [scope, setScope] = useState('all')
const [server, setServer] = useState('na')
const [partition, setPartition] = useState('parrish')
const [sending, setSending] = useState(false)
const handleSend = async () => {
if (!message.trim()) return
setSending(true)
try {
const payload: any = { message: message.trim() }
if (scope === 'server') payload.server = server
if (scope === 'partition') {
payload.server = server
payload.partition = partition
}
const res = await client.post('/broadcast', payload)
if (res.data.success) {
dispatch({ type: 'ADD_TOAST', payload: { message: 'Broadcast sent', type: 'success' } })
setMessage('')
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || 'Failed', type: 'error' } })
} finally {
setSending(false)
}
}
return (
Scope
{SCOPE_OPTIONS.map((s) => (
))}
{scope !== 'all' && (
Server
setServer(e.value[0])}>
{serverCollection.items.map((item) => (
{item.label}
))}
)}
{scope === 'partition' && (
Partition
setPartition(e.value[0])}>
{partitionCollection.items.map((item) => (
{item.label}
))}
)}
Message
)
}
function EconomyTab({ dispatch }: { dispatch: any }) {
const [playerId, setPlayerId] = useState('')
const [itemForm, setItemForm] = useState({ template_id: '', stack_size: '1', quality: '0', inventory_id: '' })
const [editForm, setEditForm] = useState({ item_id: '', field: 'stack_size', value: '' })
const post = async (endpoint: string, payload: any) => {
try {
const res = await client.post(endpoint, payload)
if (res.data.success) {
dispatch({ type: 'ADD_TOAST', payload: { message: 'Success', type: 'success' } })
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || 'Failed', type: 'error' } })
}
}
return (
Add Item
setPlayerId(e.target.value)} placeholder="Player ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setItemForm({ ...itemForm, template_id: e.target.value })} placeholder="Template ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setItemForm({ ...itemForm, stack_size: e.target.value })} placeholder="Stack Size" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setItemForm({ ...itemForm, quality: e.target.value })} placeholder="Quality" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setItemForm({ ...itemForm, inventory_id: e.target.value })} placeholder="Inventory ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
Edit Item
setEditForm({ ...editForm, item_id: e.target.value })} placeholder="Item ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setEditForm({ ...editForm, field: e.value[0] })}>
{itemFieldCollection.items.map((item) => (
{item.label}
))}
setEditForm({ ...editForm, value: e.target.value })} placeholder="New value" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
)
}
function WorldTab({ dispatch }: { dispatch: any }) {
const [tpForm, setTpForm] = useState({ fls_id: '', partition: '', x: '', y: '', z: '' })
const post = async (endpoint: string, payload: any) => {
try {
const res = await client.post(endpoint, payload)
if (res.data.success) {
dispatch({ type: 'ADD_TOAST', payload: { message: 'Success', type: 'success' } })
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || 'Failed', type: 'error' } })
}
}
return (
Teleport Player
setTpForm({ ...tpForm, fls_id: e.target.value })} placeholder="FLS ID / Account ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setTpForm({ ...tpForm, partition: e.target.value })} placeholder="Partition ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setTpForm({ ...tpForm, x: e.target.value })} placeholder="X" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setTpForm({ ...tpForm, y: e.target.value })} placeholder="Y" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setTpForm({ ...tpForm, z: e.target.value })} placeholder="Z" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
Spice Control
Partitions
)
}
function CharacterTab({ dispatch }: { dispatch: any }) {
const [charId, setCharId] = useState('')
const [newName, setNewName] = useState('')
const [demoState, setDemoState] = useState('true')
const post = async (endpoint: string, payload: any) => {
try {
const res = await client.post(endpoint, payload)
if (res.data.success) {
dispatch({ type: 'ADD_TOAST', payload: { message: 'Success', type: 'success' } })
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || 'Failed', type: 'error' } })
}
}
return (
Rename Character
setCharId(e.target.value)} placeholder="Account ID / Character ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setNewName(e.target.value)} placeholder="New Name" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
Demo State
setCharId(e.target.value)} placeholder="Account ID" bg="card.bg" borderColor="border" borderRadius="md" fontSize="sm" />
setDemoState(e.value[0])}>
{demoStateCollection.items.map((item) => (
{item.label}
))}
Danger Zone
)
}
function FunctionsTab({ dispatch }: { dispatch: any }) {
const [schema, setSchema] = useState('')
const [funcName, setFuncName] = useState('')
const [args, setArgs] = useState('{}')
const [result, setResult] = useState('')
const [running, setRunning] = useState(false)
const handleExecute = async () => {
setRunning(true)
try {
const parsedArgs = JSON.parse(args || '{}')
const res = await client.post('/functions/execute', { schema: schema || undefined, function: funcName, args: parsedArgs })
if (res.data.success) {
setResult(JSON.stringify(res.data.data, null, 2))
dispatch({ type: 'ADD_TOAST', payload: { message: 'Function executed', type: 'success' } })
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.message, type: 'error' } })
} finally {
setRunning(false)
}
}
return (
Schema (optional)
setSchema(e.target.value)}
bg="card.bg"
borderColor="border"
borderRadius="lg"
fontSize="sm"
placeholder="e.g., public"
/>
Function Name
setFuncName(e.target.value)}
bg="card.bg"
borderColor="border"
borderRadius="lg"
fontSize="sm"
placeholder="e.g., get_player_stats"
/>
Arguments (JSON)
{result && (
{result}
)}
)
}
function RawQueryTab({ dispatch }: { dispatch: any }) {
const [query, setQuery] = useState('')
const [result, setResult] = useState('')
const [running, setRunning] = useState(false)
const handleRun = async () => {
setRunning(true)
try {
const res = await client.post('/query', { query })
if (res.data.success) {
setResult(JSON.stringify(res.data.data, null, 2))
dispatch({ type: 'ADD_TOAST', payload: { message: 'Query executed', type: 'success' } })
} else {
dispatch({ type: 'ADD_TOAST', payload: { message: res.data.error || 'Failed', type: 'error' } })
}
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || e.message, type: 'error' } })
} finally {
setRunning(false)
}
}
return (
SQL Query (Read-Only SELECT)
Read Only
)
}
function AuditLogTab({ dispatch }: { dispatch: any }) {
const [limit, setLimit] = useState(50)
const [logs, setLogs] = useState([])
const [loading, setLoading] = useState(false)
const load = async () => {
setLoading(true)
try {
const res = await client.get(`/audit/logs?limit=${limit}`)
if (res.data.success) setLogs(res.data.data)
} catch (e: any) {
dispatch({ type: 'ADD_TOAST', payload: { message: e.response?.data?.error || 'Failed', type: 'error' } })
} finally {
setLoading(false)
}
}
return (
setLimit(Number(e.value[0]))}>
{limitCollection.items.map((item) => (
{item.label}
))}
Time
Admin
Action
Target
Details
{logs.map((log, i) => (
{new Date(log.timestamp).toLocaleString()}
{log.admin_name}
{log.action}
{log.target_type}: {log.target_id}
{JSON.stringify(log.details)}
))}
)
}