"use client";

import React, { useEffect, useState } from "react";
import { Subscription, User } from "@/types";
import { getSubscriptions } from "@/app/(auth)/actions";
import { useRouter } from "next/navigation";

interface GetStartedSectionButtonProps {
  user: User;
}
export const GetStartedSectionButton = ({
  user,
}: GetStartedSectionButtonProps) => {
  const router = useRouter();
  const portalWebsite = process.env
    .NEXT_PUBLIC_PORTAL_SIGN365_WEBSITE as string;
  const [subscription, setSubscription] = useState<Subscription>();
  useEffect(() => {
    (async () => {
      if (!user) {
        return;
      }
      const subscriptions = await getSubscriptions();
      if (subscriptions.length > 0) {
        setSubscription(subscriptions[0]);
      }
    })();
  }, [user]);

  return subscription ? (
    <a href={portalWebsite}>
      <button className="btn text-base capitalize !rounded-md text-white bg-bg-gray-925 hover:bg-gray-900 w-full sm:w-auto mt-8">
        Management Portal
      </button>
    </a>
  ) : (
    <button
      onClick={() => router.push("/pricing")}
      className="px-10 py-2 text-base capitalize !rounded-3xl text-white bg-gradient-to-r from-pink-default to-purple-default hover:bg-gray-900 w-full sm:w-auto"
    >
      Upgrade
    </button>
  );
};

export default GetStartedSectionButton;
