import { getAuthOpenAIHeaders } from "../utils";

interface IDefaultPayload {
  token: string;
  method: "POST" | "GET";
  path: string;
}

interface IMessagePayload {
  role: string;
  content: string;
  file_ids?: string[];
}

export interface IMessage {
  id: string;
  object: "thread.message" | string;
  created_at: number;
  thread_id: string;
  role: "user" | string;
  content: IMessageContent[];
  file_ids: unknown[];
  assistant_id: null | unknown;
  run_id: null | unknown;
  metadata: object;
}

interface IMessageContent {
  type: "text" | string;
  text: {
    value: string;
    annotations: unknown[];
  };
}

// Create thread
export interface PayloadCreateThread extends IDefaultPayload {
  messages: IMessagePayload[];
}

export interface ResponseCreateThread {
  id: string;
  object: string;
  created_at: number;
  metadata: object;
}
// Create thread end

// Create message
export interface PayloadCreateMessage extends IDefaultPayload {
  role: "user";
  content: string;
}

export type ResponseCreateMessage = IMessage;
// Create message end

// Get list messages
export interface PayloadGetListMessages extends IDefaultPayload {
  QueryParams?: {
    limit?: number;
    order?: "asc" | "desc";
    after?: string;
    before?: string;
  };
}

export interface ResponseGetListMessages {
  object: "list" | string;
  data: IMessage[];
  first_id: string;
  last_id: string;
  has_more: boolean;
}
// Get list messages end

// Run thread
interface ITool {
  type: "code_interpreter" | string;
}

interface IUsage {
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
}

export interface PayloadRunThread extends IDefaultPayload {
  assistant_id: string;
}

export interface ResponseRunThread {
  id: string;
  object: "thread.run" | string;
  created_at: number;
  assistant_id: string;
  thread_id: string;
  status: "queued" | string;
  started_at: number;
  expires_at: null | unknown;
  cancelled_at: null | unknown;
  failed_at: null | unknown;
  completed_at: number;
  last_error: null | unknown;
  model: "gpt-4" | string;
  instructions: null | unknown;
  tools: ITool[];
  file_ids: string[];
  metadata: object;
  usage: null | IUsage;
}
// Run thread end

// Get status run thread
export type PayloadGetStatusRunThread = IDefaultPayload;

export type ResponseGetStatusRunThread = ResponseRunThread;
// Get status run thread end

// Get list runs
export type PayloadGetListRuns = IDefaultPayload;

export interface ResponseGetListRuns {
  object: "list" | string;
  data: ResponseRunThread[];
  first_id: string;
  last_id: string;
  has_more: boolean;
}
// Get list runs end

export type Payload =
  | PayloadCreateThread
  | PayloadCreateMessage
  | PayloadGetListMessages
  | PayloadRunThread
  | PayloadGetStatusRunThread;

export function createRequest({
  token,
  method,
  path,
  ...data
}: Payload): Request {
  const url = new URL(path);
  const body = JSON.stringify({ ...data });
  if ("QueryParams" in data && data.QueryParams) {
    for (const [key, value] of Object.entries(data.QueryParams)) {
      url.searchParams.set(key, String(value));
    }
  }
  if (method === "GET") {
    return new Request(url, {
      method: method,
      headers: getAuthOpenAIHeaders(token),
    });
  }

  return new Request(url, {
    method: method,
    headers: getAuthOpenAIHeaders(token),
    body,
  });
}
