"use client";

import Container from "@/components/elements/Container";
import { IAdvertising, IAdvertisingType } from "@/types/types";
import useAxios from "@/hooks/useAxios";
import React, { useEffect, useState } from "react";
import MainBillboardCard from "@/components/billboards/MainBillboardCard/MainBillboardCard";
import RoundedDiv from "@/components/elements/RoundedDiv";
import RoundedButton from "@/components/elements/RoundedButton";
import LocalePageShell from "@/components/i18n/LocalePageShell";
import { getBillboardDisplayTypeLabel } from "@/lib/billboards/displayTypeLabel";
import { useSearchParams } from "next/navigation";
import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";

function SuccessBillboard() {
  const { t } = useTranslation("common");
  const { request } = useAxios();
  const searchParams = useSearchParams();
  const projectId = searchParams.get("projectId");

  const [advertising, setAdvertising] = useState<IAdvertising>();
  const [typeList, setTypeList] = useState<IAdvertisingType[] | null>(null);
  const [price, setPrice] = useState("");

  useEffect(() => {
    const fetchAd = async () => {
      const response = await request<{ advertising: IAdvertising }>(
        "GET",
        `/advertising/get/web/${projectId}`
      );
      setAdvertising(response?.advertising);
    };
    const fetchStates = async () => {
      try {
        const response = await request<{
          advertisingTypes: IAdvertisingType[];
        }>("GET", "/advertising/types");
        setTypeList(response?.advertisingTypes || null);
      } catch (err) {
        console.log(err);
      }
    };
    fetchAd();
    fetchStates();
  }, [projectId, request]);

  useEffect(() => {
    if (advertising?.type && typeList) {
      const foundType = typeList.find((item) => item.name === advertising.type);
      if (foundType) {
        setPrice(String(foundType.price));
      }
    }
  }, [advertising, typeList]);

  return (
    <LocalePageShell>
      <Container>
        <h6 className="font-bold md:text-xl text-lg text-center mt-2 line-clamp-2 mb-4 text-[#17A600] ">
          {t("billboards.payment.success")}
        </h6>
        <div className="flex flex-col items-center text-sm">
          <Image
            width={50}
            height={50}
            alt={"verify icon"}
            src={`/images/icons/success.svg`}
            className="pb-1 mb-5"
          />
        </div>
        <div className="px-4 w-full text-sm md:text-md font-semibold mt-10">
          {advertising && <MainBillboardCard billboard={advertising} />}
          <div className="flex flex-col items-center gap-4 mt-4">
            <RoundedDiv className="p-2 w-full">
              {getBillboardDisplayTypeLabel(t, advertising?.type)}:{" "}
              {Number(price).toLocaleString()} {t("settings.toman")}
            </RoundedDiv>
            <p className="my-5">{t("billboards.payment.registeredMessage")}</p>
            <Link href={"/settings/my-billboards"}>
              <RoundedButton variant="primary" className="w-32 h-9">
                {t("billboards.payment.myBillboards")}
              </RoundedButton>
            </Link>
          </div>
        </div>
      </Container>
    </LocalePageShell>
  );
}

export default SuccessBillboard;
