export type NitterFeedConfig = {
  username: string;
  label?: string;
  agentId?: string;
  pollIntervalMs?: number;
  deliveryChannel?: string;
  deliveryTo?: string;
  filter?: string;
  enabled: boolean;
};

export type NitterDeliveryMode = "session" | "channel" | "both";

export type NitterPluginConfig = {
  enabled: boolean;
  instanceUrl: string;
  pollIntervalMs: number;
  maxItemsPerPoll: number;
  requestTimeoutMs: number;
  deliveryChannel?: string;
  deliveryTo?: string;
  deliveryMode: NitterDeliveryMode;
  feeds: NitterFeedConfig[];
};

function asString(v: unknown, fallback: string): string {
  return typeof v === "string" && v.trim() ? v.trim() : fallback;
}

function asNumber(v: unknown, fallback: number): number {
  return typeof v === "number" && Number.isFinite(v) && v > 0 ? v : fallback;
}

function asBool(v: unknown, fallback: boolean): boolean {
  return typeof v === "boolean" ? v : fallback;
}

function parseDeliveryMode(v: unknown): NitterDeliveryMode {
  if (v === "session" || v === "channel" || v === "both") return v;
  return "both";
}

function parseFeedConfig(raw: unknown): NitterFeedConfig | null {
  if (!raw || typeof raw !== "object") return null;
  const obj = raw as Record<string, unknown>;
  const username = typeof obj.username === "string" ? obj.username.trim() : "";
  if (!username) return null;
  return {
    username,
    label: typeof obj.label === "string" ? obj.label.trim() || undefined : undefined,
    agentId: typeof obj.agentId === "string" ? obj.agentId.trim() || undefined : undefined,
    pollIntervalMs:
      typeof obj.pollIntervalMs === "number" && obj.pollIntervalMs > 0
        ? obj.pollIntervalMs
        : undefined,
    deliveryChannel:
      typeof obj.deliveryChannel === "string" ? obj.deliveryChannel.trim() || undefined : undefined,
    deliveryTo: typeof obj.deliveryTo === "string" ? obj.deliveryTo.trim() || undefined : undefined,
    filter: typeof obj.filter === "string" ? obj.filter.trim() || undefined : undefined,
    enabled: asBool(obj.enabled, true),
  };
}

export function parseNitterConfig(raw: unknown): NitterPluginConfig {
  const obj = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
  const feeds: NitterFeedConfig[] = [];
  if (Array.isArray(obj.feeds)) {
    for (const entry of obj.feeds) {
      const parsed = parseFeedConfig(entry);
      if (parsed) feeds.push(parsed);
    }
  }
  return {
    enabled: asBool(obj.enabled, true),
    instanceUrl: asString(obj.instanceUrl, "http://localhost:8080").replace(/\/+$/, ""),
    pollIntervalMs: asNumber(obj.pollIntervalMs, 300_000),
    maxItemsPerPoll: asNumber(obj.maxItemsPerPoll, 20),
    requestTimeoutMs: asNumber(obj.requestTimeoutMs, 15_000),
    deliveryChannel:
      typeof obj.deliveryChannel === "string" ? obj.deliveryChannel.trim() || undefined : undefined,
    deliveryTo: typeof obj.deliveryTo === "string" ? obj.deliveryTo.trim() || undefined : undefined,
    deliveryMode: parseDeliveryMode(obj.deliveryMode),
    feeds,
  };
}
