"use client";

import RoundedButton from "@/components/elements/RoundedButton";
import useAxios from "@/hooks/useAxios";
import Link from "next/link";
import { useRouter } from "next/navigation";
import React from "react";

function MyBillboardStatusActions({
  status,
  reject_reason,
  _id,
  type,
  showDiscount,
  title,
}: {
  status?: string;
  reject_reason?: string;
  _id: string;
  type?: string;
  showDiscount?: boolean;
  title?: string;
}) {
  const router = useRouter();
  const { request } = useAxios();
  const payHandler = async () => {
    try {
      const response = await request<{ authority?: string }>(
        "POST",
        "/advertising/initiate-payment-web",
        {
          item_name: type, // Pass advertisingId to initiate payment
          advertisingId: _id,
          showDiscount: showDiscount,
        }
      );

      if (type === "free") {
        router.push("/settings/my-billboards");
      } else {
        const authority = response.authority; // Get the payment authority
        const paymentUrl = `https://www.zarinpal.com/pg/StartPay/${authority}`; // Construct payment URL
        router.push(paymentUrl);
      }
    } catch (error: unknown) {
      console.log(error);
    }
  };
  const republishHandler = async () => {
    try {
      const response = await request<{ authority?: string }>(
        "POST",
        "/advertising/republish-payment/web",
        {
          item_name: type, // Pass advertisingId to initiate payment
          advertisingId: _id,
          showDiscount: showDiscount,
        }
      );

      if (type === "free") {
        router.push("/settings/my-billboards");
      } else {
        const authority = response.authority; // Get the payment authority
        const paymentUrl = `https://www.zarinpal.com/pg/StartPay/${authority}`; // Construct payment URL
        router.push(paymentUrl);
      }
    } catch (error: unknown) {
      console.log(error);
    }
  };
  return (
    <div className="flex flex-col items-center gap-4 text-center">
      {status == "rejected" ? (
        <span>
          علت رد شدن ویترین: <span>{reject_reason}</span>
        </span>
      ) : (
        ""
      )}
      {status == "pre_payment" ? (
        <div className="mt-5 flex justify-center">
          <RoundedButton
            onClick={payHandler}
            className="w-24 h-9 !border-[#0C8002] text-[#0C8002]"
          >
            پرداخت
          </RoundedButton>
        </div>
      ) : (
        ""
      )}
      {status == "pre_payment" || status == "paid" || status == "rejected" ? (
        <Link href={`/settings/my-billboards/edit/${_id}/${title}`}>
          <RoundedButton className="w-24 h-9 !border-[#0C8002] text-[#0C8002]">
            ویرایش
          </RoundedButton>
        </Link>
      ) : (
        ""
      )}
      {status == "accepted" ? (
        <div
          onClick={republishHandler}
          className="mt-5 flex flex-col justify-center items-center gap-2"
        >
          <span>
            قبل از منقضی شدن ویترین خود، آن را بازنشر کنید تا ۳۰٪ از مبلغ ساخت
            ویترین جدید را تخفیف بگیرید
          </span>
          <RoundedButton className="w-24 h-9 !border-[#0C8002] text-[#0C8002]">
            بازنشر
          </RoundedButton>
        </div>
      ) : (
        ""
      )}
      {status == "expired" ? (
        <div onClick={republishHandler} className="mt-5 flex justify-center">
          <RoundedButton
            onClick={republishHandler}
            className="w-24 h-9 !border-[#0C8002] text-[#0C8002]"
          >
            بازنشر
          </RoundedButton>
        </div>
      ) : (
        ""
      )}
    </div>
  );
}

export default MyBillboardStatusActions;
