import { defineStore } from "pinia";
import { ref, computed } from "vue";

export interface Connection {
  id: number;
  name: string;
  hostname: string;
  port: number;
  protocol: "ssh" | "rdp";
  groupId: number;
  tags?: string[];
}

export interface Group {
  id: number;
  name: string;
  parentId: number | null;
}

/**
 * Connection store.
 * Manages connections, groups, and search state.
 *
 * Uses mock data until Wails bindings are connected.
 */
export const useConnectionStore = defineStore("connection", () => {
  const connections = ref<Connection[]>([
    { id: 1, name: "Asgard", hostname: "192.168.1.4", port: 22, protocol: "ssh", groupId: 1, tags: ["Prod"] },
    { id: 2, name: "Docker", hostname: "155.254.29.221", port: 22, protocol: "ssh", groupId: 1, tags: ["Prod"] },
    { id: 3, name: "Predator Mac", hostname: "192.168.1.214", port: 22, protocol: "ssh", groupId: 1 },
    { id: 4, name: "CLT-VMHOST01", hostname: "100.64.1.204", port: 3389, protocol: "rdp", groupId: 1 },
    { id: 5, name: "ITFlow", hostname: "192.154.253.106", port: 22, protocol: "ssh", groupId: 2 },
    { id: 6, name: "Mautic", hostname: "192.154.253.112", port: 22, protocol: "ssh", groupId: 2 },
  ]);

  const groups = ref<Group[]>([
    { id: 1, name: "Vantz's Stuff", parentId: null },
    { id: 2, name: "MSPNerd", parentId: null },
  ]);

  const searchQuery = ref("");

  /** Filter connections by search query. */
  const filteredConnections = computed(() => {
    const q = searchQuery.value.toLowerCase().trim();
    if (!q) return connections.value;
    return connections.value.filter(
      (c) =>
        c.name.toLowerCase().includes(q) ||
        c.hostname.toLowerCase().includes(q) ||
        c.tags?.some((t) => t.toLowerCase().includes(q)),
    );
  });

  /** Get connections belonging to a specific group. */
  function connectionsByGroup(groupId: number): Connection[] {
    const q = searchQuery.value.toLowerCase().trim();
    const groupConns = connections.value.filter((c) => c.groupId === groupId);
    if (!q) return groupConns;
    return groupConns.filter(
      (c) =>
        c.name.toLowerCase().includes(q) ||
        c.hostname.toLowerCase().includes(q) ||
        c.tags?.some((t) => t.toLowerCase().includes(q)),
    );
  }

  /** Check if a group has any matching connections (for search filtering). */
  function groupHasResults(groupId: number): boolean {
    return connectionsByGroup(groupId).length > 0;
  }

  /** Load connections from backend (mock for now). */
  async function loadConnections(): Promise<void> {
    // TODO: replace with Wails binding — ConnectionService.ListConnections()
    // connections.value = await ConnectionService.ListConnections();
  }

  /** Load groups from backend (mock for now). */
  async function loadGroups(): Promise<void> {
    // TODO: replace with Wails binding — ConnectionService.ListGroups()
    // groups.value = await ConnectionService.ListGroups();
  }

  return {
    connections,
    groups,
    searchQuery,
    filteredConnections,
    connectionsByGroup,
    groupHasResults,
    loadConnections,
    loadGroups,
  };
});
