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

interface Payload {
    token: string;
}

export interface PayloadGet extends Payload {
    placementKey: EPlacementKeys;
}

export enum EPlacementKeys {
    "aura.placement.v1.mike" = "aura.placement.v1.mike",
    "aura.placement.main" = "aura.placement.main",
    "aura.placement.redesign.main" = "aura.placement.redesign.main",
    "aura.placement.email.marketing" = "aura.placement.email.marketing",
    "aura.placement.secret.discount" = "aura.placement.secret.discount",
    "aura.placement.palmistry.main" = "aura.placement.palmistry.main",
    "aura.placement.palmistry.redesign" = "aura.placement.palmistry.redesign",
    "aura.placement.chat" = "aura.placement.chat",
    "aura.placement.email.palmistry" = "aura.placement.email.palmistry",
    "aura.placement.email.palmistry.discount" = "aura.placement.email.palmistry.discount",
    "aura.placement.email.compatibility.discount" = "aura.placement.email.compatibility.discount",
    "aura.placement.palmistry.secret.discount" = "aura.placement.palmistry.secret.discount",
    "aura.placement.compatibility.v2" = "aura.placement.compatibility.v2",
    "aura.placement.compatibility.v2.secret.discount" = "aura.placement.compatibility.v2.secret.discount",
    "aura.placement.compatibility.v3" = "aura.placement.compatibility.v3",
    "aura.placement.compatibility.v3.secret.discount" = "aura.placement.compatibility.v3.secret.discount",
    "aura.placement.compatibility.v4" = "aura.placement.compatibility.v4",
    "aura.placement.compatibility.v4.secret.discount" = "aura.placement.compatibility.v4.secret.discount",
    "aura.placement.payment" = "aura.placement.payment" // anonymous payment
}

export interface ResponseGetSuccess {
    placementId: string,
    paywallId: string,
    paywall: IPaywall;
    currency: string;
}

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

export interface IPaywall {
    _id: string;
    key: string;
    name: string;
    products: IPaywallProduct[];
    properties: IPaywallProperties[];
}

export interface IPaywallProduct {
    _id: string;
    key: string;
    productId: string;
    name: string;
    priceId: string;
    type: string;
    description: string;
    discountPrice: null;
    discountPriceId: null;
    isDiscount: boolean;
    isFreeTrial: boolean;
    isTrial: boolean;
    price: number;
    trialDuration: number | null;
    trialPrice: number | null;
    trialPriceId: string | null;
    currency?: string;
}

interface IPaywallProperties {
    _id: string;
    key: string;
    value: string;
}

export type ResponseGet = ResponseGetSuccess | ResponseGetError;

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