import { computed, type Ref } from 'vue';
import { useChatStore } from '../store/chat';
import type { Agent } from './agents';

export function useAgentDisplay(
    selectedAgent: Ref<string>,
    defaultAgent: Ref<string>,
    allAgents: Ref<Agent[]>,
) {
    const chatStore = useChatStore();

    const defaultAgentName = computed(() => {
        const agent = allAgents.value.find(a => a.id === defaultAgent.value);
        return agent ? agent.name : defaultAgent.value;
    });

    const agentDisplayName = computed(() => {
        const agent = allAgents.value.find(a => a.id === selectedAgent.value);
        return (agent ? agent.name : selectedAgent.value).toUpperCase();
    });

    const isAgentRunning  = computed(() => chatStore.smState === 'AGENT_RUNNING');
    const agentStatusDone = computed(() => chatStore.channelState === 'READY' || chatStore.channelState === 'FRESH');

    const agentStatus = computed<string | null>(() => {
        switch (chatStore.smState) {
            case 'CONNECTING':       return '⚙️ Connecting…';
            case 'AGENT_RUNNING':    return '⚙️ Working…';
            case 'HANDOVER_PENDING': return '📝 Writing handover…';
            case 'HANDOVER_DONE':    return '✅ Handover ready';
            case 'SWITCHING':        return '🔀 Switching…';
            default:                 return null;
        }
    });

    return { defaultAgentName, agentDisplayName, isAgentRunning, agentStatusDone, agentStatus };
}
