import { ArrowLeft } from "lucide-react";
import type { GetServerSideProps } from "next";
import Link from "next/link";
import { useRouter } from "next/router";
import PaymentStatus from "~/components/payment_status";
import { ProgressRedirect } from "~/components/progress_redirect";
import { Button } from "~/components/ui/button";
import stripe from "~/lib/stripe";
import { redirectTo500 } from "~/lib/utils";
import { useTranslation } from "~/providers/I18nProvider";
import OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";

enum AcquistoStatusEnum {
	success = "success",
	processing = "processing",
	failed = "failed",
	internal_error = "internal_error",
	not_found = "not_found",
}

type AcquistoProcessingProps = {
	redirectUrl: string;
	status: AcquistoStatusEnum;
};

export const getServerSideProps = (async (context) => {
	const paymentIntentId = context.query?.payment_intent;
	if (!paymentIntentId || typeof paymentIntentId !== "string") {
		return redirectTo500;
	}

	const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
	if (!paymentIntent) {
		return {
			props: {
				redirectUrl: "",
				status: AcquistoStatusEnum.internal_error,
			},
		};
	}

	const access_token = context.req.cookies.access_token;

	const helper = await TrpcAuthedFetchingIstance({ access_token });
	if (helper) {
		const payment = await helper.trpc.pagamenti.getPaymentFromIntent.fetch({
			pIntentId: paymentIntent.id,
		});

		if (!payment) {
			return {
				props: {
					redirectUrl: "",
					status: AcquistoStatusEnum.not_found,
				},
			};
		}

		let resultStatus: AcquistoStatusEnum;
		if (paymentIntent.status === "succeeded") {
			if (payment.paymentstatus !== PaymentStatusEnum.success) {
				resultStatus = AcquistoStatusEnum.internal_error;
			}
			resultStatus = AcquistoStatusEnum.success;
		} else if (paymentIntent.status === "processing") {
			if (payment.paymentstatus !== PaymentStatusEnum.processing) {
				resultStatus = AcquistoStatusEnum.internal_error;
			}
			resultStatus = AcquistoStatusEnum.processing;
		} else {
			resultStatus = AcquistoStatusEnum.failed;
		}
		return {
			props: {
				redirectUrl:
					payment.type === OrderTypeEnum.Rinnovo
						? `/area-riservata/rinnovo/${payment.rinnovo_id}`
						: `/area-riservata/servizio/${payment.servizio_id}`,
				status: resultStatus,
			},
		};
	}

	return redirectTo500;
}) satisfies GetServerSideProps<AcquistoProcessingProps>;

/**
 * Pagina di stato del pagamento dopo un acquisto: /servizio/pagamento-status?payment_intent=paymentIntentId
 */
export default function AcquistoProcessing({
	redirectUrl,
	status,
}: AcquistoProcessingProps) {
	const { t } = useTranslation();
	const router = useRouter();
	return (
		<div className="mx-auto my-5 w-full max-w-lg space-y-8">
			<h3 className="my-8 text-center font-bold text-neutral-700 text-xl uppercase tracking-wide dark:text-white">
				{t.acquisto_elaborazione.titolo}
			</h3>

			<div className="space-y-4 p-4">
				{status === AcquistoStatusEnum.success && (
					<>
						<PaymentStatus
							message={t.acquisto_elaborazione.success_desc}
							status="success"
							title={t.acquisto_elaborazione.success_title}
						/>

						<ProgressRedirect href={redirectUrl} />
					</>
				)}
				{status === AcquistoStatusEnum.processing && (
					<>
						<PaymentStatus
							message={t.acquisto_elaborazione.processing_desc}
							status="processing"
							title={t.acquisto_elaborazione.processing_title}
						/>
						<Link href="/area-riservata/dashboard">
							<Button className="w-full" variant="info">
								{t.acquisto_elaborazione.to_dashboard}
							</Button>
						</Link>
					</>
				)}
				{status === AcquistoStatusEnum.failed && (
					<>
						<PaymentStatus
							message={t.acquisto_elaborazione.failed_desc}
							status="error"
							title={t.acquisto_elaborazione.failed_title}
						/>
						<Link href="/area-riservata/chat">
							<Button className="w-full">
								{t.acquisto_elaborazione.to_chat}
							</Button>
						</Link>
					</>
				)}
				{status === AcquistoStatusEnum.internal_error && (
					<>
						<PaymentStatus
							message={t.acquisto_elaborazione.internal_error_desc}
							status="error"
							title={t.acquisto_elaborazione.internal_error_title}
						/>
						<Link href="/area-riservata/chat">
							<Button className="w-full">
								{t.acquisto_elaborazione.to_chat}
							</Button>
						</Link>
					</>
				)}
				{status === AcquistoStatusEnum.not_found && (
					<>
						<PaymentStatus
							message={t.acquisto_elaborazione.not_found_desc}
							status="error"
							title={t.acquisto_elaborazione.not_found_title}
						/>
						<Button
							className="w-full"
							onClick={() => router.back()}
							variant="secondary"
						>
							<ArrowLeft />
							{t.indietro}
						</Button>
						<Link href="/area-riservata/chat">
							<Button className="w-full">
								{t.acquisto_elaborazione.to_chat}
							</Button>
						</Link>
					</>
				)}
			</div>
		</div>
	);
}
