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

interface Payload {
  token: string;
}

export type PayloadGet = Payload;

export interface IPaymentInfo {
  productId: string;
  key: string;
}

export interface PayloadPost extends Payload {
  data: {
    user: {
      id: string;
      email: string;
      name: string;
      sign: string;
      age: number;
      gender?: string;
    };
    partner: {
      sign: string | null;
      age: number | null;
    };
    paymentInfo: IPaymentInfo;
    return_url: string;
  };
}

export interface ResponseGet {
  _id: string,
  key: string,
  name: string,
  type: string,
  description: string,
  discountPrice: null | unknown,
  isDiscount: boolean,
  isFreeTrial: boolean,
  isTrial: boolean,
  price: number,
  trialDuration: number,
  trialPrice: number
}

interface ResponsePostNewPaymentData {
  paymentIntent: {
    status: string;
    data: {
      client_secret: string;
      paymentIntentId: string;
      return_url?: string;
      public_key: string;
      product: {
        id: string;
        name: string;
        description: null | string;
        price: {
          id: string;
          unit_amount: number;
          currency: string;
        };
      };
    };
  };
}

interface ResponsePostExistPaymentData {
  payment: {
    status: string;
    invoiceId: string;
  };
}

interface ResponsePostError {
  status: string;
  message: string;
}

export type ResponsePost =
  | ResponsePostNewPaymentData
  | ResponsePostExistPaymentData
  | ResponsePostError;

export const createRequestPost = ({ data, token }: PayloadPost): Request => {
  const url = new URL(routes.server.dApiPaymentCheckout());
  const body = JSON.stringify(data);
  return new Request(url, {
    method: "POST",
    headers: getAuthHeaders(token),
    body,
  });
};

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