import React from "react";
import { View, Text, StyleSheet, Pressable } from "react-native";
import ChevronRight from "../../assets/icons/chevron_rightside.svg";
import { useTranslation } from "react-i18next";

type Props = {
  warrantyStartDate: string;
  warrantyEndDate: string;
};

const BatteryWarrantyCard: React.FC<Props> = ({
  warrantyStartDate,
  warrantyEndDate,
}) => {
  const start = new Date(warrantyStartDate);
  const end = new Date(warrantyEndDate);
  const now = new Date();

  const totalDuration = end.getTime() - start.getTime();
  const elapsed = now.getTime() - start.getTime();
  const remaining = Math.max(end.getTime() - now.getTime(), 0);

  const progress = Math.min(elapsed / totalDuration, 1);

  const yearsLeft = Math.floor(remaining / (365.25 * 24 * 60 * 60 * 1000));
  const monthsLeft = Math.floor(
    (remaining % (365.25 * 24 * 60 * 60 * 1000)) / (30.44 * 24 * 60 * 60 * 1000)
  );
  const daysLeft = Math.floor(
    (remaining % (30.44 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000)
  );

  const { t } = useTranslation();

  return (
    <View style={styles.card}>
      <View style={styles.topRow}>
        <View style={styles.textColumn}>
          <Text style={styles.title}>{t("home.battery-warranty-left")}</Text>
          <Text style={styles.time}>
            {(() => {
              const parts: string[] = [];

              if (yearsLeft > 0) {
                parts.push(`${yearsLeft} ${t("home.year")}`);
              }

              if (monthsLeft > 0) {
                parts.push(`${monthsLeft} ${t("home.month")}`);
              }

              if (daysLeft > 0 || parts.length === 0) {
                parts.push(`${daysLeft} ${t("home.day")}`);
              }

              return parts.join(", ");
            })()}
          </Text>
        </View>
        <Pressable style={styles.iconButton}>
          <ChevronRight />
        </Pressable>
      </View>
      <View style={styles.progressContainer}>
        <View style={styles.progressBar}>
          <View
            style={[styles.progressFill, { width: `${(1 - progress) * 100}%` }]}
          />
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  card: {
    backgroundColor: "#FCFCFC",
    borderRadius: 10,
    padding: 16,
    width: "100%",
    height: 96,
    justifyContent: "space-between",
  },
  topRow: {
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "flex-start",
  },
  textColumn: {
    flexDirection: "column",
    gap: 4,
  },
  title: {
    fontSize: 12,
    fontFamily: "Inter-Medium",
    color: "#565E70",
    lineHeight: 16,
  },
  time: {
    fontSize: 14,
    fontFamily: "Inter-SemiBold",
    color: "#252A34",
    lineHeight: 20,
  },
  iconButton: {
    width: 40,
    height: 40,
    padding: 10,
    justifyContent: "center",
    alignItems: "center",
  },
  progressContainer: {
    marginTop: 8,
    width: "100%",
    height: 8,
  },
  progressBar: {
    width: "100%",
    height: 8,
    backgroundColor: "#E5E9F0",
    borderRadius: 10,
    overflow: "hidden",
  },
  progressFill: {
    height: "100%",
    backgroundColor: "#027A48",
    borderRadius: 10,
  },
});

export default BatteryWarrantyCard;
