import {
  readFileSync,
  existsSync,
} from "node:fs";
import { join, dirname } from "node:path";
import { ensureSecureDir, writeSecureFile } from "./fs-permissions.js";
import { ZENCODE_DIR } from "./config.js";

// ─── Types ───────────────────────────────────────────────────────────────────

export interface StoredLinearIssue {
  id: string;
  identifier: string;
  title: string;
  description: string;
  url: string;
  branchName: string;
  priorityLabel: string;
  stateName: string;
  stateType: string;
  teamName: string;
  teamKey: string;
  teamId: string;
  assigneeName?: string;
  updatedAt?: string;
}

// ─── Paths ───────────────────────────────────────────────────────────────────

const DEFAULT_PATH = join(ZENCODE_DIR, "session-linear-issues.json");

// ─── Store ───────────────────────────────────────────────────────────────────

let issues: Record<string, StoredLinearIssue> = {};
let loaded = false;
let filePath = DEFAULT_PATH;

function ensureLoaded(): void {
  if (loaded) return;
  try {
    if (existsSync(filePath)) {
      const raw = readFileSync(filePath, "utf-8");
      issues = JSON.parse(raw) as Record<string, StoredLinearIssue>;
    }
  } catch {
    issues = {};
  }
  loaded = true;
}

function persist(): void {
  ensureSecureDir(dirname(filePath));
  writeSecureFile(filePath, JSON.stringify(issues, null, 2));
}

// ─── Public API ──────────────────────────────────────────────────────────────

export function getLinearIssue(sessionId: string): StoredLinearIssue | undefined {
  ensureLoaded();
  return issues[sessionId];
}

export function setLinearIssue(sessionId: string, issue: StoredLinearIssue): void {
  ensureLoaded();
  issues[sessionId] = issue;
  persist();
}

export function removeLinearIssue(sessionId: string): void {
  ensureLoaded();
  delete issues[sessionId];
  persist();
}

export function getAllLinearIssues(): Record<string, StoredLinearIssue> {
  ensureLoaded();
  return { ...issues };
}

/** Reset internal state and optionally set a custom file path (for testing). */
export function _resetForTest(customPath?: string): void {
  issues = {};
  loaded = false;
  filePath = customPath || DEFAULT_PATH;
}
