import routes from "@/routes";
import { getAuthHeaders, getBaseHeaders } from "../utils";
import { ICreateAuthorizeResponse } from "./User";
import { AddressFields } from "@/hooks/payment/nmi/useAddressFields";

export interface Payload {
    token: string;
}

export interface PayloadPost extends Payload {
    productId: string;
    placementId: string;
    paywallId: string;
    paymentToken?: string;
    address?: AddressFields;
}

export interface PayloadPostAnonymous {
    productId: string;
    placementId: string;
    paywallId: string;
    paymentToken: string;
    sessionId: string;
    address?: AddressFields;
}

interface ResponsePostSuccess {
    status: "payment_intent_created" | "paid" | unknown,
    type: "setup" | "payment",
    data: {
        client_secret: string,
        paymentIntentId: string,
        return_url?: string,
        public_key: string,
        product: {
            id: string,
            name: string,
            description?: string,
            price: {
                id: string,
                unit_amount: number,
                currency: "USD" | string
            }
        }
    }
}

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

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

export type ResponsePost = ResponsePostSuccess | ResponsePostSinglePaymentSuccess | ResponsePostError;

export interface ResponsePostAnonymousSuccess {
    user: ICreateAuthorizeResponse;
    status: string;
    invoiceId: string;
}

export const createRequestPost = ({ token, productId, placementId, paywallId, paymentToken, address }: PayloadPost): Request => {
    const url = new URL(routes.server.makePayment());
    const body = JSON.stringify({
        productId,
        placementId,
        paywallId,
        paymentToken,
        address
    });
    return new Request(url, { method: "POST", headers: getAuthHeaders(token), body });
};

export const createRequestPostAnonymous = ({ productId, placementId, paywallId, paymentToken, sessionId, address }: PayloadPostAnonymous): Request => {
    const url = new URL(routes.server.makeAnonymousPayment());
    const body = JSON.stringify({
        productId,
        placementId,
        paywallId,
        paymentToken,
        sessionId,
        address
    });
    return new Request(url, { method: "POST", headers: getBaseHeaders(), body });
};

export interface IPaymentConfigResponse {
    status: "success" | string,
    data: {
        nmi: {
            publicKey: string
        }
    }
}

export const getConfigRequest = (): Request => {
    const url = new URL(routes.server.getPaymentConfig());
    return new Request(url, { method: "GET", headers: getBaseHeaders() });
};

export interface IPaymentMethodsResponse {
    status: "success" | "error",
    message: string,
}

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