"use client";

import { Link } from "react-router-dom";
import { MapPin, Maximize, BedDouble, Bath } from "lucide-react";
import FavoriteButton from "@/components/FavoriteButton";
import type { Property } from "@/data/types";

const tagColors: Record<string, string> = {
  Премиум: "bg-accent text-accent-foreground",
  "Нова обява": "bg-sea text-primary-foreground",
  "Намалена цена": "bg-destructive text-destructive-foreground",
  Ексклузивно: "bg-primary text-primary-foreground",
};

const formatPrice = (price: number, dealType: string) => {
  const formatted = new Intl.NumberFormat("bg-BG").format(price);
  return dealType === "rent" ? `€${formatted}/мес.` : `€${formatted}`;
};

interface FeaturedPropertiesProps {
  properties: Property[];
}

const FeaturedProperties = ({ properties }: FeaturedPropertiesProps) => {
  return (
    <section className="pt-8 md:pt-32 pb-20 bg-secondary/50">
      <div className="container mx-auto px-4 lg:px-8">
        <div className="flex items-end justify-between mb-12">
          <div>
            <h2 className="section-title mb-3">Топ предложения</h2>
            <p className="text-muted-foreground text-lg font-body">
              Ръчно подбрани имоти от нашите експерти
            </p>
          </div>
          <Link
            to="/properties"
            className="hidden md:inline-flex items-center gap-1 text-sm font-semibold text-accent hover:text-accent/80 transition-colors font-body"
          >
            Виж всички →
          </Link>
        </div>

        <div className="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
          {properties.map((p, i) => (
            <Link
              to={`/properties/${p.id}`}
              key={p.id}
              className="animate-fade-in-up"
              style={{ animationDelay: `${i * 0.1}s` }}
            >
              <article className="property-card group">
                <div className="relative aspect-[4/3] overflow-hidden">
                  <img
                    src={p.images[0]}
                    alt={p.title}
                    className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-700"
                    loading="lazy"
                  />
                  <div className="absolute top-3 left-3 flex gap-2">
                    {p.tags.slice(0, 1).map((tag) => (
                      <span
                        key={tag}
                        className={`text-xs font-semibold px-3 py-1 rounded-full ${
                          tagColors[tag] || "bg-primary text-primary-foreground"
                        }`}
                      >
                        {tag}
                      </span>
                    ))}
                  </div>
                  <FavoriteButton
                    className="absolute top-3 right-3 h-8 w-8 bg-card/80 backdrop-blur-sm hover:bg-card"
                    iconClassName="w-4 h-4"
                    propertyId={p.id}
                  />
                </div>

                <div className="p-4">
                  <div className="flex items-center gap-1.5 text-muted-foreground text-xs mb-2 font-body">
                    <MapPin className="w-3.5 h-3.5" />
                    <span>{p.location.city}, {p.location.region}</span>
                  </div>
                  <h3 className="font-semibold text-foreground mb-2 font-body text-sm leading-snug line-clamp-2">
                    {p.title}
                  </h3>
                  <div className="flex items-center gap-3 text-xs text-muted-foreground mb-3 font-body">
                    <span className="flex items-center gap-1">
                      <Maximize className="w-3.5 h-3.5" />
                      {p.area} кв.м.
                    </span>
                    {p.bedrooms > 0 && (
                      <span className="flex items-center gap-1">
                        <BedDouble className="w-3.5 h-3.5" />
                        {p.bedrooms}
                      </span>
                    )}
                    {p.bathrooms > 0 && (
                      <span className="flex items-center gap-1">
                        <Bath className="w-3.5 h-3.5" />
                        {p.bathrooms}
                      </span>
                    )}
                  </div>
                  <div className="flex items-center justify-between pt-3 border-t border-border">
                    <span className="text-lg font-bold text-primary font-display">
                      {formatPrice(p.price, p.dealType)}
                    </span>
                    <span className="text-xs text-muted-foreground font-body">
                      ID: {p.id}
                    </span>
                  </div>
                </div>
              </article>
            </Link>
          ))}
        </div>

        <div className="mt-8 text-center md:hidden">
          <Link to="/properties" className="cta-button inline-block">
            Виж всички имоти
          </Link>
        </div>
      </div>
    </section>
  );
};

export default FeaturedProperties;
