import { create } from 'zustand';

type ConnectionStatus = 'connected' | 'connecting' | 'disconnected';

interface TerminalConnection {
  serverId: string;
  status: ConnectionStatus;
}

interface TerminalState {
  activeTab: string | null;
  connections: Record<string, TerminalConnection>;
  setActiveTab: (tab: string) => void;
  addConnection: (id: string, serverId: string) => void;
  updateConnectionStatus: (id: string, status: ConnectionStatus) => void;
  removeConnection: (id: string) => void;
}

export const useTerminalStore = create<TerminalState>((set) => ({
  activeTab: null,
  connections: {},

  setActiveTab: (tab) => set({ activeTab: tab }),

  addConnection: (id, serverId) =>
    set((state) => ({
      connections: {
        ...state.connections,
        [id]: { serverId, status: 'connecting' },
      },
    })),

  updateConnectionStatus: (id, status) =>
    set((state) => ({
      connections: {
        ...state.connections,
        [id]: { ...state.connections[id], status },
      },
    })),

  removeConnection: (id) =>
    set((state) => {
      const connections = { ...state.connections };
      delete connections[id];
      return { connections };
    }),
}));
