"use client";

import * as React from "react";
import {
  Select,
  SelectTrigger,
  SelectContent,
  SelectItem,
  SelectValue,
} from "@/components/ui/select";
import { useTranslations } from "next-intl";
import { PricingPlan, pricingPlansConfig } from "./packagesConfig";
import { CheckIcon, SaudiRiyal } from "lucide-react";

export default function PlanSummary() {
  const currency = <SaudiRiyal className="h-6 h-6 inline" />;
  const t = useTranslations("subscribe");
  const tForm = useTranslations("subscribe.forms");
  const tPackages = useTranslations("packages");
  const translators = {
    basic: useTranslations("packages.packages.basic"),
    standard: useTranslations("packages.packages.standard"),
    enterprise: useTranslations("packages.packages.enterprise"),
  };

  const plans: PricingPlan[] = pricingPlansConfig.map((plan) => {
    const tPlan =
      translators[plan.translationNamespace as keyof typeof translators];

    if (!tPlan) {
      throw new Error(
        `No translator found for namespace: ${plan.translationNamespace}`
      );
    }

    return {
      name: tPlan("title"),
      price: plan.price,
      isPopular: plan.isPopular,
      features: tPackages.raw(
        `packages.${plan.translationNamespace}.features`
      ) as string[],
    };
  });

  const [selected, setSelected] = React.useState<PricingPlan>(plans[0]);

  return (
    <div className="p-4 md:p-10 bg-bg md:rounded-[24px] rounded-[16px] space-y-5">
      <h3 className="text-text text-[24px] font-bold">{t("planSummary")}</h3>

      <div className="space-y-6">
        <div className="flex flex-col md:flex-row gap-2 p-[10px] rounded-[8px] border border-secondary-main bg-white">
          <div className="flex items-center gap-2 w-full">
            <label className="text-[18px] font-bold text-text whitespace-nowrap">
              {tForm("packageName")}
            </label>
            <Select
              onValueChange={(value) => setSelected(plans[Number(value)])}
              defaultValue="0"
            >
              <SelectTrigger className="w-full max-w-[232px] border border-[#DFE3E8] color-[#5F6972]">
                <SelectValue placeholder={tForm("choosePackage")} />
              </SelectTrigger>
              <SelectContent>
                {plans.map((plan, index) => (
                  <SelectItem key={index} value={String(index)}>
                    {plan.name}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>
          <div className="flex items-center gap-2 shrink-0">
            <span className="text-[18px] font-bold text-text whitespace-nowrap">
              {tForm("packageValue")}
            </span>
            <strong className="text-[32px] font-bold text-text whitespace-nowrap">
              {selected.price}{currency}/
              <span className="text-[18px]">{tForm("month")}</span>
            </strong>
          </div>
        </div>
        <div className="space-y-6">
          <h4 className="text-[18px] font-bold text-text">
            {tForm("packageFeatures")}
          </h4>
          <ul className="grid grid-cols-1 md:grid-cols-2 gap-3">
            {selected.features.map((feature, i) => (
              <li
                key={i}
                className="flex items-center gap-2 text-[20px] font-bold text-text"
              >
                <span className="w-6 h-6 flex items-center justify-center rounded-full bg-secondary-16 text-secondary-main">
                  <CheckIcon className="w-4" />
                </span>
                {feature}
              </li>
            ))}
          </ul>
        </div>
      </div>
    </div>
  );
}
