import routes from "@/routes";
import { getAuthHeaders } from "../utils";

const dateFormatter = (date: string): string => {
  return date
    .split("-")
    .map((item) => item.padStart(2, "0"))
    .join("-");
};

interface IBestiesHoroscopePayload {
  birthDate: string;
  sign: string;
}

interface IMyHoroscopePayload {
  birthDate: string;
  sign: string;
  period: string;
}

interface IPredictionMoonsPayload {
  birthDate: string;
  sign: string;
  period: string;
}

interface IThermalPayload {
  birthDate: string;
  sign: string;
  period: string;
}

interface IMoonPhaseTrackerPayload {
  birthDate: string;
  sign: string;
  period: string;
}

interface IEnergyVampirismPayload {
  birthDate: string;
  sign: string;
  period: string;
}

export type AIRequestPayload =
  | IBestiesHoroscopePayload
  | IPredictionMoonsPayload
  | IMyHoroscopePayload
  | IThermalPayload
  | IMoonPhaseTrackerPayload
  | IEnergyVampirismPayload;

export interface Payload {
  promptKey: string;
  aiRequest: AIRequestPayload;
  token: string;
}

export interface PayloadGet {
  id: string;
  token: string;
}

export interface Response {
  ai_request: IAiResponse;
  meta: IMeta;
}

interface IMeta {
  links: {
    self: string;
  };
}

export interface IAiResponse {
  id: string;
  prompt_key: string;
  created_at: string;
  updated_at: string;
  state: string;
  is_reused: boolean;
  finished_at: string;
  job_id: null;
  response: IAIRequest;
}

export interface IAiResponseGet {
  ai_request: IAIRequest;
}

export interface IAIRequest {
  id: string;
  prompt_key: string;
  created_at: string;
  updated_at: string;
  state: string;
  is_reused: boolean;
  finished_at: string;
  job_id: unknown;
  response: {
    id: string;
    inputs: {
      sign: string;
      birth_date: string;
    };
    created_at: string;
    updated_at: string;
    body: string;
    has_vocal: boolean;
    speech: unknown;
  };
}

const getBody = (aiRequest: AIRequestPayload, promptKey: string): string => {
  if (["horoscope_besties"].includes(promptKey)) {
    return JSON.stringify({
      ai_request: {
        birth_date: dateFormatter(aiRequest.birthDate),
        sign: aiRequest.sign,
      },
    });
  }
  if (
    [
      "horoscope_name",
      "prediction_moons",
      "compatibility_thermal",
      "moonse_phase",
      "energy_vampirism",
    ].includes(promptKey)
  ) {
    return JSON.stringify({
      ai_request: {
        birth_date: dateFormatter(aiRequest.birthDate),
        sign: aiRequest.sign,
        period: (aiRequest as IPredictionMoonsPayload).period,
      },
    });
  }
  return JSON.stringify({ aiRequest });
};

export const createRequest = ({
  promptKey,
  aiRequest,
  token,
}: Payload): Request => {
  const url = new URL(routes.server.aiRequestsV2(promptKey));
  const body = getBody(aiRequest, promptKey);
  return new Request(url, {
    method: "POST",
    headers: getAuthHeaders(token),
    body,
  });
};

export const createRequestGet = ({ id, token }: PayloadGet): Request => {
  const url = new URL(routes.server.getAiRequestsV2(id));
  return new Request(url, {
    method: "GET",
    headers: getAuthHeaders(token),
  });
};
