import { EventEmitter } from "events";
import Database from "better-sqlite3";
import { SessionManager } from "../../../core/session/session-manager.js";
import { FrameManager } from "../../../core/context/frame-manager.js";
import { LinearTaskReader } from "./linear-task-reader.js";
import { existsSync } from "fs";
function getEnv(key, defaultValue) {
  const value = process.env[key];
  if (value === void 0) {
    if (defaultValue !== void 0) return defaultValue;
    throw new Error(`Environment variable ${key} is required`);
  }
  return value;
}
function getOptionalEnv(key) {
  return process.env[key];
}
class DataService extends EventEmitter {
  db = null;
  sessionManager = null;
  frameManager = null;
  taskReader = null;
  linearMappings = /* @__PURE__ */ new Map();
  cache = /* @__PURE__ */ new Map();
  cacheTimeout = 5e3;
  // 5 seconds
  async initialize() {
    try {
      try {
        const dbPath = process.env["PROJECT_ROOT"] ? `${process.env["PROJECT_ROOT"]}/.stackmemory/context.db` : `${process.cwd()}/.stackmemory/context.db`;
        if (existsSync(dbPath)) {
          this.db = new Database(dbPath);
        }
      } catch (dbError) {
        if (process.env["DEBUG"]) {
          console.log("Database not accessible, continuing without it");
        }
      }
      try {
        this.taskReader = new LinearTaskReader(
          process.env["PROJECT_ROOT"] || process.cwd()
        );
        this.linearMappings = this.taskReader.getMappings();
        if (process.env["DEBUG"]) {
          const tasks = this.taskReader.getTasks();
          console.log(
            `LinearTaskReader initialized with ${tasks.length} tasks`
          );
        }
      } catch (tsError) {
        if (process.env["DEBUG"]) {
          console.log("Task reader initialization failed:", tsError.message);
        }
      }
      try {
        this.sessionManager = new SessionManager({
          enableMonitoring: true
        });
      } catch (smError) {
        if (process.env["DEBUG"]) {
          console.log("SessionManager initialization failed:", smError.message);
        }
      }
      if (this.db) {
        try {
          this.frameManager = new FrameManager(this.db, "tui");
        } catch (fmError) {
          if (process.env["DEBUG"]) {
            console.log("FrameManager initialization failed:", fmError.message);
          }
        }
      }
      if (process.env["DEBUG"]) {
        console.log(
          "TUI: Using local task store only (no direct Linear API calls)"
        );
      }
      this.emit("data:ready");
    } catch (error) {
      if (process.env["DEBUG"]) {
        console.error("DataService initialization error:", error);
      }
      this.emit("data:ready");
    }
  }
  async getSessions() {
    const cached = this.getFromCache("sessions");
    if (cached) return cached;
    try {
      const activeSessions = this.sessionManager?.getActiveSessions() || [];
      let recentSessions = [];
      if (this.db) {
        try {
          const stmt = this.db.prepare(`
            SELECT * FROM sessions 
            WHERE created_at > datetime('now', '-24 hours')
            ORDER BY created_at DESC
            LIMIT 20
          `);
          recentSessions = stmt.all() || [];
        } catch (dbError) {
          if (process.env["DEBUG"]) {
            console.log("Sessions table not found, using mock data");
          }
        }
      }
      if (activeSessions.length === 0 && recentSessions.length === 0) {
        const mockSessions = this.getMockSessions();
        this.setCache("sessions", mockSessions);
        return mockSessions;
      }
      const sessions = [
        ...activeSessions.map(this.formatSession),
        ...recentSessions.map(this.formatDatabaseSession)
      ];
      this.setCache("sessions", sessions);
      return sessions;
    } catch (error) {
      if (process.env["DEBUG"]) {
        console.error("Error getting sessions:", error);
      }
      return this.getMockSessions();
    }
  }
  async getTasks() {
    const cached = this.getFromCache("tasks");
    if (cached) return cached;
    const tasks = [];
    if (this.taskReader) {
      try {
        const localTasks = this.taskReader.getTasks();
        tasks.push(...localTasks);
        if (process.env["DEBUG"]) {
          console.log(
            `Loaded ${tasks.length} tasks from local store (no Linear API calls)`
          );
        }
      } catch (error) {
        if (process.env["DEBUG"]) {
          console.log("Failed to get local tasks:", error.message);
        }
      }
    }
    if (tasks.length === 0) {
      console.log(
        '\u2139\uFE0F  No local tasks found. Run "npm run linear:sync" to sync tasks from Linear.'
      );
      const mockTasks = this.getMockTasks();
      this.setCache("tasks", mockTasks);
      return mockTasks;
    }
    this.setCache("tasks", tasks);
    return tasks;
  }
  async getFrames() {
    const cached = this.getFromCache("frames");
    if (cached) return cached;
    try {
      const frames = this.frameManager?.getAllFrames() || [];
      if (frames.length === 0) {
        const mockFrames = this.getMockFrames();
        this.setCache("frames", mockFrames);
        return mockFrames;
      }
      const formatted = frames.map(this.formatFrame);
      this.setCache("frames", formatted);
      return formatted;
    } catch (error) {
      if (process.env["DEBUG"]) {
        console.error("Error getting frames:", error);
      }
      const mockFrames = this.getMockFrames();
      this.setCache("frames", mockFrames);
      return mockFrames;
    }
  }
  async getAgents() {
    const cached = this.getFromCache("agents");
    if (cached) return cached;
    try {
      const agents = [
        {
          id: "agent-1",
          type: "analyzer",
          status: "active",
          currentTask: {
            id: "task-1",
            description: "Analyzing codebase for performance issues",
            progress: 0.65,
            startTime: Date.now() - 12e4
          },
          tasksCompleted: 42,
          tasksFailed: 3,
          averageTime: 18e4,
          successRate: 0.93,
          cpuUsage: 45,
          memoryUsage: 62,
          tokenUsage: 125e3
        },
        {
          id: "agent-2",
          type: "builder",
          status: "idle",
          tasksCompleted: 28,
          tasksFailed: 1,
          averageTime: 24e4,
          successRate: 0.96,
          cpuUsage: 12,
          memoryUsage: 38,
          tokenUsage: 85e3
        },
        {
          id: "agent-3",
          type: "tester",
          status: "error",
          tasksCompleted: 15,
          tasksFailed: 5,
          averageTime: 9e4,
          successRate: 0.75,
          lastError: {
            message: "Test suite timeout",
            timestamp: Date.now() - 6e4,
            recoverable: true
          },
          cpuUsage: 0,
          memoryUsage: 25,
          tokenUsage: 45e3
        }
      ];
      this.setCache("agents", agents);
      return agents;
    } catch (error) {
      this.emit("error", error);
      return [];
    }
  }
  async getPRs() {
    const cached = this.getFromCache("prs");
    if (cached) return cached;
    try {
      const prs = [
        {
          id: "pr-1",
          number: 142,
          title: "feat: Add TUI monitoring dashboard",
          state: "open",
          draft: false,
          author: { login: "stackmemory-bot" },
          reviews: [{ user: "reviewer1", state: "approved" }],
          checks: {
            total: 5,
            passed: 3,
            failed: 0,
            pending: 2
          },
          createdAt: new Date(Date.now() - 36e5).toISOString(),
          updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
          additions: 1250,
          deletions: 85,
          changedFiles: 12,
          comments: 3,
          labels: ["enhancement", "monitoring"],
          linearTask: "STA-100"
        }
      ];
      this.setCache("prs", prs);
      return prs;
    } catch (error) {
      this.emit("error", error);
      return [];
    }
  }
  async getIssues() {
    const cached = this.getFromCache("issues");
    if (cached) return cached;
    try {
      const issues = [
        {
          id: "issue-1",
          number: 89,
          title: "Session monitoring improvements needed",
          state: "open",
          author: { login: "user1" },
          assignees: ["stackmemory-bot"],
          labels: ["enhancement", "monitoring"],
          createdAt: new Date(Date.now() - 864e5).toISOString(),
          updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
          comments: 5
        }
      ];
      this.setCache("issues", issues);
      return issues;
    } catch (error) {
      this.emit("error", error);
      return [];
    }
  }
  async getAnalytics() {
    const cached = this.getFromCache("analytics");
    if (cached) return cached;
    try {
      const analytics = {
        sessions: {
          labels: ["1h", "2h", "3h", "4h", "5h", "6h"],
          values: [5, 8, 12, 15, 18, 22]
        },
        tokens: {
          labels: ["1h", "2h", "3h", "4h", "5h", "6h"],
          values: [12e3, 25e3, 45e3, 62e3, 78e3, 95e3]
        },
        tasks: {
          completed: 45,
          inProgress: 12,
          todo: 28,
          velocity: [8, 12, 15, 18, 20]
        },
        quality: {
          testsPassed: 142,
          testsFailed: 3,
          coverage: 78,
          lintErrors: 0
        },
        performance: {
          avgResponseTime: [120, 115, 108, 105, 110],
          errorRate: [0.02, 0.015, 0.01, 8e-3, 5e-3],
          throughput: [100, 120, 135, 140, 145]
        }
      };
      this.setCache("analytics", analytics);
      return analytics;
    } catch (error) {
      this.emit("error", error);
      return {
        sessions: { labels: [], values: [] },
        tokens: { labels: [], values: [] },
        tasks: { completed: 0, inProgress: 0, todo: 0, velocity: [] },
        quality: { testsPassed: 0, testsFailed: 0, coverage: 0, lintErrors: 0 },
        performance: { avgResponseTime: [], errorRate: [], throughput: [] }
      };
    }
  }
  formatSession(session) {
    return {
      id: session.id,
      startTime: session.startTime,
      lastActivity: session.lastActivity,
      completed: session.completed,
      error: session.error,
      totalTokens: session.totalTokens,
      contextUsage: session.contextUsage || 0,
      filesEdited: session.filesEdited,
      commandsRun: session.commandsRun,
      errors: session.errors,
      primaryFile: session.primaryFile,
      gitBranch: session.gitBranch,
      lastCommit: session.lastCommit,
      linearTask: session.linearTask,
      agentType: session.agentType,
      recentActivities: session.recentActivities
    };
  }
  formatDatabaseSession(row) {
    return {
      id: row.id,
      startTime: new Date(row.created_at).getTime(),
      lastActivity: row.updated_at ? new Date(row.updated_at).getTime() : void 0,
      completed: row.status === "completed",
      totalTokens: row.token_count,
      contextUsage: row.context_usage || 0,
      filesEdited: row.files_edited ? JSON.parse(row.files_edited) : [],
      commandsRun: row.commands_run || 0
    };
  }
  formatLinearTask(task) {
    return {
      id: task.id,
      identifier: task.identifier,
      title: task.title,
      description: task.description,
      // Use Linear state.type for stable mapping ('unstarted'|'started'|...)
      state: task.state?.type || task.state?.name,
      priority: task.priority,
      estimate: task.estimate,
      dueDate: task.dueDate,
      assignee: task.assignee ? {
        id: task.assignee.id,
        name: task.assignee.name,
        email: task.assignee.email
      } : void 0,
      labels: task.labels?.nodes.map((l) => l.name),
      project: task.project ? {
        id: task.project.id,
        name: task.project.name,
        key: task.project.key
      } : void 0
    };
  }
  formatRestLinearTask(task) {
    const stateMap = {
      backlog: "Backlog",
      unstarted: "To Do",
      started: "In Progress",
      completed: "Done",
      canceled: "Canceled"
    };
    return {
      id: task.id,
      identifier: task.identifier,
      title: task.title,
      description: task.description,
      state: stateMap[task.state.type] || task.state.name,
      priority: task.priority,
      estimate: task.estimate,
      assignee: task.assignee ? task.assignee.name : void 0,
      createdAt: task.createdAt,
      updatedAt: task.updatedAt
    };
  }
  formatFrame(frame) {
    return {
      id: frame.id,
      sessionId: frame.sessionId,
      parentId: frame.parentId,
      type: frame.type || "leaf",
      inputs: frame.inputs,
      outputs: frame.outputs,
      tools: frame.tools,
      digest: frame.digest,
      timestamp: frame.timestamp,
      tokenCount: frame.tokenCount || 0,
      tier: frame.tier || "hot",
      compressionRatio: frame.compressionRatio,
      score: frame.score,
      children: frame.children,
      references: frame.references
    };
  }
  getFromCache(key) {
    const cached = this.cache.get(key);
    if (cached && Date.now() - cached.timestamp < this.cacheTimeout) {
      return cached.data;
    }
    return null;
  }
  setCache(key, data) {
    this.cache.set(key, { data, timestamp: Date.now() });
  }
  mapLocalStatusToLinearState(status) {
    const statusMap = {
      pending: "To Do",
      in_progress: "In Progress",
      completed: "Done",
      cancelled: "Canceled",
      blocked: "To Do"
    };
    return statusMap[status] || "To Do";
  }
  mapLocalPriorityToLinear(priority) {
    const priorityMap = {
      urgent: 0,
      high: 1,
      medium: 2,
      low: 3
    };
    return priorityMap[priority] || 2;
  }
  cleanup() {
    if (this.db) {
      this.db.close();
    }
    this.cache.clear();
  }
  // Mock data methods for demo/offline mode
  getMockSessions() {
    const now = Date.now();
    return [
      {
        id: "demo-session-1",
        startTime: now - 81e5,
        // 2h 15m ago
        lastActivity: now - 6e4,
        // 1 minute ago
        completed: false,
        totalTokens: 45e3,
        contextUsage: 0.45,
        // 45%
        filesEdited: ["src/components/Dashboard.tsx", "src/hooks/useAuth.ts"],
        commandsRun: 23,
        primaryFile: "src/components/Dashboard.tsx",
        gitBranch: "feature/tui-dashboard",
        agentType: "frontend",
        recentActivities: [
          {
            timestamp: now - 12e4,
            type: "file_edit",
            description: "Modified Dashboard.tsx"
          },
          {
            timestamp: now - 6e4,
            type: "command",
            description: "npm run test"
          }
        ]
      },
      {
        id: "demo-session-2",
        startTime: now - 27e5,
        // 45m ago
        lastActivity: now - 3e5,
        // 5 minutes ago
        completed: false,
        totalTokens: 22e3,
        contextUsage: 0.22,
        // 22%
        filesEdited: ["src/api/endpoints.ts", "src/middleware/auth.ts"],
        commandsRun: 15,
        primaryFile: "src/api/endpoints.ts",
        gitBranch: "main",
        agentType: "backend",
        recentActivities: [
          {
            timestamp: now - 6e5,
            type: "file_edit",
            description: "Updated API endpoints"
          }
        ]
      }
    ];
  }
  getMockFrames() {
    return [
      {
        id: "frame-1",
        sessionId: "demo-session-1",
        type: "root",
        tier: "hot",
        timestamp: (/* @__PURE__ */ new Date()).toISOString(),
        tokenCount: 2500,
        score: 95,
        compressionRatio: 2.3,
        digest: "Implementing React component with hooks",
        tools: ["Edit", "Read", "MultiEdit"],
        inputs: ["Create user dashboard"],
        outputs: ["Dashboard component created"],
        children: [],
        references: []
      },
      {
        id: "frame-2",
        sessionId: "demo-session-1",
        type: "branch",
        tier: "warm",
        timestamp: new Date(Date.now() - 36e5).toISOString(),
        tokenCount: 1800,
        score: 78,
        compressionRatio: 1.8,
        digest: "Setting up API endpoints",
        tools: ["Write", "Edit"],
        inputs: ["Setup REST API"],
        outputs: ["API routes configured"],
        children: [],
        references: []
      }
    ];
  }
  getMockTasks() {
    return [
      {
        id: "task-1",
        identifier: "STA-101",
        title: "Implement TUI dashboard",
        state: "In Progress",
        priority: 2,
        assignee: "demo-user",
        createdAt: new Date(Date.now() - 864e5).toISOString(),
        updatedAt: (/* @__PURE__ */ new Date()).toISOString()
      },
      {
        id: "task-2",
        identifier: "STA-102",
        title: "Add terminal compatibility",
        state: "Done",
        priority: 1,
        assignee: "demo-user",
        createdAt: new Date(Date.now() - 1728e5).toISOString(),
        updatedAt: (/* @__PURE__ */ new Date()).toISOString()
      }
    ];
  }
  getMockAgents() {
    return [
      {
        id: "agent-1",
        name: "code-reviewer",
        type: "reviewer",
        status: "idle",
        tasksCompleted: 45,
        averageTime: "2.3s",
        successRate: 98.5,
        lastActive: (/* @__PURE__ */ new Date()).toISOString()
      },
      {
        id: "agent-2",
        name: "test-runner",
        type: "qa",
        status: "active",
        tasksCompleted: 156,
        averageTime: "5.6s",
        successRate: 95.2,
        lastActive: (/* @__PURE__ */ new Date()).toISOString()
      }
    ];
  }
  getMockPRs() {
    return [
      {
        id: "pr-1",
        number: 123,
        title: "feat: Add terminal compatibility layer",
        state: "open",
        author: { login: "demo-user" },
        assignees: [],
        labels: ["enhancement", "tui"],
        createdAt: new Date(Date.now() - 864e5).toISOString(),
        updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
        comments: 3,
        reviews: 1,
        checks: {
          total: 5,
          passed: 5,
          failed: 0,
          pending: 0
        }
      }
    ];
  }
}
export {
  DataService
};
//# sourceMappingURL=data-service.js.map
