"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";

import AutoTopUpToggle from "@/components/domains/profile/AutoTopUpToggle/AutoTopUpToggle";
import { Button, Typography } from "@/components/ui";
import { useUserBalance } from "@/hooks/balance/useUserBalance";
import { ROUTES } from "@/shared/constants/client-routes";

import styles from "./Billing.module.scss";

function Billing() {
  const t = useTranslations("Profile.billing");
  const router = useRouter();
  const { balance, isLoading } = useUserBalance();

  // Round balance to nearest integer when available
  const roundedBalance = balance !== null ? Math.round(balance) : null;

  const onBilling = () => {
    router.push(ROUTES.profileSubscriptions());
  };

  return (
    <div className={styles.container}>
      <Button className={styles.button} onClick={onBilling}>
        <Typography size="xl" color="white">
          {t("billing_button")}
        </Typography>
      </Button>
      <div className={styles.credits}>
        <Typography as="p" weight="bold" color="white" align="left">
          {t("credits.title", {
            credits:
              isLoading || roundedBalance === null
                ? "..."
                : String(roundedBalance),
          })}
        </Typography>
        <Typography
          className={styles.creditsDescription}
          as="p"
          size="sm"
          color="white"
          align="left"
        >
          {t("credits.description")}
        </Typography>
      </div>
      <AutoTopUpToggle />
      <Typography
        as="p"
        weight="bold"
        align="left"
        className={styles.anyQuestions}
      >
        {t.rich("any_questions", {
          link: chunks => (
            <Link
              href="https://witlab.us"
              target="_blank"
              rel="noopener noreferrer"
            >
              {chunks}
            </Link>
          ),
          linkText: t("any_questions_link"),
        })}
      </Typography>
      <Typography
        as="p"
        align="left"
        color="secondary"
        className={styles.subscriptionUpdate}
      >
        {t.rich("subscription_update", {
          bold: chunks => (
            <Typography weight="bold" color="secondary">
              {chunks}
            </Typography>
          ),
          subscriptionUpdateBold: t("subscription_update_bold"),
          br: () => <br />,
        })}
      </Typography>
    </div>
  );
}

export default Billing;
