
import { useCurrency } from "@/hooks/useCurrency";
import { useTranslations } from "@/providers/TranslationProviders";
import { Text, View } from "react-native";

export interface InvestmentTermsProps {
  roi?: number;
  share_value?: number;
  number_of_shares?: number;
  share_sold?: number;
  effective_date?: string;
  maturity_date?: string | null;
  roi_distribution?: string;
  marketplace_resale?: boolean;
  share_type?: string;
}

export default function InvestmentTerms({
  roi = 0,
  share_value = 0,
  number_of_shares = 0,
  share_sold = 0,
  effective_date = "",
  maturity_date = null,
  roi_distribution = "monthly",
  marketplace_resale = false,
  share_type = "fixed",
}: InvestmentTermsProps) {
  const { tran, translateNumber } = useTranslations();
  const { currency } = useCurrency();

  let durationMonths = 24; 
  if (effective_date && maturity_date) {
    const start = new Date(effective_date);
    const end = new Date(maturity_date);
    const diffTime = Math.abs(end.getTime() - start.getTime());
    durationMonths = Math.ceil(diffTime / (1000 * 60 * 60 * 24 * 30));
  }

  return (
    <View className="mb-8">
      <View className="flex-row items-center justify-between mb-4 border-b border-slate-100 pb-4">
        <Text className="text-xl font-manrope-extrabold text-slate-900">{tran("Investment Terms")}</Text>
      </View>
      <View className="bg-slate-50 rounded-2xl p-5 border border-slate-100/50 shadow-sm">
        {/* ROI */}
        <View className="flex-row justify-between items-center py-3 border-b border-slate-200/50">
          <Text className="text-sm font-manrope-medium text-slate-500">{tran("ROI")}</Text>
          <View className="items-end">
            <Text className="text-lg font-manrope-extrabold text-slate-900 leading-tight">{translateNumber(roi)}%</Text>
            <Text className="text-[10px] font-manrope-medium text-slate-400">{tran("p.a.")} • {tran("Projected")}</Text>
          </View>
        </View>
        {/* Duration */}
        <View className="flex-row justify-between items-center py-4 border-b border-slate-200/50">
          <Text className="text-sm font-manrope-medium text-slate-500">{tran("Duration")}</Text>
          <Text className="text-sm font-manrope-bold text-slate-900">
            {share_type === "infinite" ? "∞" : maturity_date ? `${translateNumber(durationMonths)} ${tran("Months")}` : "Fixed"}
          </Text>
        </View>
        {/* Shares */}
        <View className="flex-row justify-between items-center py-4 border-b border-slate-200/50">
          <View>
            <Text className="text-sm font-manrope-medium text-slate-500">{tran("Shares")}</Text>
            <Text className="text-[10px] font-manrope-medium text-slate-400 mt-1">
               {translateNumber(share_sold)} {tran("sold")} • {translateNumber(number_of_shares - share_sold)} {tran("available")}
            </Text>
          </View>
          <Text className="text-sm font-manrope-bold text-slate-900">{translateNumber(number_of_shares)}</Text>
        </View>
        {/* Price / Share */}
        <View className="flex-row justify-between items-center py-4 border-b border-slate-200/50">
          <Text className="text-sm font-manrope-medium text-slate-500">{tran("Price / Share")}</Text>
          <Text className="text-sm font-manrope-bold text-slate-900">
            {currency.symbol}{translateNumber(share_value)}
          </Text>
        </View>
        {/* Payouts */}
        <View className="flex-row justify-between items-center py-4 border-b border-slate-200/50">
          <Text className="text-sm font-manrope-medium text-slate-500">{tran("Payouts")}</Text>
          <Text className="text-sm font-manrope-bold text-slate-900 capitalize">
            {share_type === "fixed" ? tran("At Maturity") : tran(roi_distribution === "monthly" ? "Monthly" : "Yearly")}
          </Text>
        </View>
        {/* Marketplace */}
        <View className="flex-row justify-between items-center py-4">
          <Text className="text-sm font-manrope-medium text-slate-500">{tran("Marketplace Resale")}</Text>
          <Text className="text-sm font-manrope-bold text-slate-900 capitalize">
            {marketplace_resale ? tran("Enabled") : tran("Disabled")}
          </Text>
        </View>
      </View>
    </View>
  );
}
