import { SerializedNodes } from '@craftjs/core';

export type SitesmithStatus =
  | 'OK_BONUS' | 'OK_MONTHLY'
  | 'DISABLED' | 'CAP_REACHED'
  | 'USER_KILLSWITCH' | 'SERVER_KILLSWITCH'
  | 'RATE_LIMITED' | 'BLOCKED' | 'AI_ERROR' | 'AI_INVALID';

export interface SitesmithSummary {
  enabled: boolean;
  monthly_cap: number;
  monthly_used: number;
  bonus_credits: number;
  resets_on: string;
  status: SitesmithStatus;
}

export interface SitesmithMessage {
  role: 'user' | 'assistant';
  content: string;
  response_type: 'replace' | 'patch' | 'ask' | 'error' | null;
  created_at: string;
}

export interface SerializedTreeNode {
  type: { resolvedName: string };
  props: Record<string, unknown> & { aiName?: string; node_id?: string };
  nodes?: SerializedTreeNode[];
}

export type SitesmithPatchOp =
  | { op: 'update_props'; node_id: string; props: Record<string, unknown> }
  | { op: 'replace_node'; node_id: string; tree: SerializedTreeNode }
  | { op: 'insert_after'; node_id: string; tree: SerializedTreeNode }
  | { op: 'insert_before'; node_id: string; tree: SerializedTreeNode }
  | { op: 'delete_node'; node_id: string };

export type SitesmithResponse =
  | { type: 'replace'; scope: 'site' | 'page' | 'section';
      pages: Array<{ name: string; tree: SerializedTreeNode }>;
      header?: { tree: SerializedTreeNode };
      footer?: { tree: SerializedTreeNode };
      message: string; }
  | { type: 'patch'; ops: SitesmithPatchOp[]; message: string; }
  | { type: 'ask';   question: string; options?: string[]; };

export interface SendResultOk  { ok: true;  response: SitesmithResponse; }
export interface SendResultErr { ok: false; status: SitesmithStatus | 'BLOCKED'; message: string; }
export type SendResult = SendResultOk | SendResultErr;
