import React from 'react';
import { Box, Text } from 'ink';
import { LogEntry } from './types.js';

interface DaemonFeedProps {
    logs: LogEntry[];
    secureMode: boolean;
}

const LEVEL_COLOR: Record<LogEntry['level'], string> = {
    info: 'gray',
    warn: 'yellow',
    error: 'red',
    command: 'cyan',
    frank: 'magenta',
    guardian: 'blue',
    hive: 'cyan',
};

const LEVEL_PREFIX: Record<LogEntry['level'], string> = {
    info: '  ',
    warn: '⚠ ',
    error: '✖ ',
    command: '» ',
    frank: 'F ',
    guardian: 'G ',
    hive: 'H ',
};

export function DaemonFeed({ logs, secureMode }: DaemonFeedProps) {
    return (
        <Box
            flexDirection="column"
            borderStyle="round"
            borderColor={secureMode ? 'red' : 'gray'}
            paddingX={1}
            paddingY={0}
            flexGrow={1}
        >
            <Box justifyContent="space-between">
                <Text bold color="gray">◈ DAEMON FEED</Text>
                {secureMode && <Text bold color="red">⚠ SECURE OVERLAY ACTIVE</Text>}
            </Box>

            <Box flexDirection="column" marginTop={1}>
                {logs.slice(-14).map(log => (
                    <Box key={log.id} gap={1}>
                        <Text dimColor>{log.timestamp}</Text>
                        <Text color={LEVEL_COLOR[log.level] as any} bold={log.level === 'warn' || log.level === 'error'}>
                            {LEVEL_PREFIX[log.level]}{log.message}
                        </Text>
                    </Box>
                ))}
            </Box>
        </Box>
    );
}
