"use client";

import React from "react";
import Link from "next/link";
import { Formatting } from "@customer-portal/domain/toolkit";
import type { InvoiceItem } from "@customer-portal/domain/billing";

const { formatCurrency } = Formatting;

interface InvoiceItemsProps {
  items?: InvoiceItem[];
  currency: string;
}

export function InvoiceItems({ items = [], currency }: InvoiceItemsProps) {
  const hasServiceConnection = (item: InvoiceItem) => {
    return Boolean(item.serviceId) && Number(item.serviceId) > 0;
  };

  // Calculate what types of items are actually present
  const hasLinkedItems = items.some(item => hasServiceConnection(item));
  const hasOneTimeItems = items.some(item => !hasServiceConnection(item));

  const renderItemContent = (item: InvoiceItem) => {
    const isLinked = hasServiceConnection(item);

    const itemContent = (
      <div
        className={`flex justify-between items-start py-4 px-4 rounded-xl transition-all duration-200 ${
          isLinked
            ? "hover:bg-primary/5 cursor-pointer group border border-transparent hover:border-primary/20"
            : "bg-muted/30"
        }`}
      >
        <div className="flex-1 pr-4">
          <div className="flex items-start gap-3">
            <div className="flex-1">
              <div
                className={`font-semibold mb-1 ${
                  isLinked ? "text-primary group-hover:text-primary/80" : "text-foreground"
                }`}
              >
                {item.description}
                {isLinked && (
                  <svg
                    className="inline-block w-4 h-4 ml-1 text-primary/70 group-hover:text-primary"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
                    />
                  </svg>
                )}
              </div>
              <div className="flex flex-wrap gap-3 text-sm">
                {item.quantity && item.quantity > 1 && (
                  <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-primary/10 text-primary">
                    Qty: {item.quantity}
                  </span>
                )}
                {isLinked ? (
                  <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-success/10 text-success">
                    <svg className="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                      <path
                        fillRule="evenodd"
                        d="M12.586 4.586a2 2 0 112.828 2.828l-3 3a2 2 0 01-2.828 0 1 1 0 00-1.414 1.414 4 4 0 005.656 0l3-3a4 4 0 00-5.656-5.656l-1.5 1.5a1 1 0 101.414 1.414l1.5-1.5zm-5 5a2 2 0 012.828 0 1 1 0 101.414-1.414 4 4 0 00-5.656 0l-3 3a4 4 0 105.656 5.656l1.5-1.5a1 1 0 10-1.414-1.414l-1.5 1.5a2 2 0 11-2.828-2.828l3-3z"
                        clipRule="evenodd"
                      />
                    </svg>
                    Subscription #{item.serviceId}
                  </span>
                ) : (
                  <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-muted text-muted-foreground">
                    <svg className="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                      <path
                        fillRule="evenodd"
                        d="M4 4a2 2 0 00-2 2v8a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2H4zm0 2v8h12V6H4z"
                        clipRule="evenodd"
                      />
                    </svg>
                    One-time item
                  </span>
                )}
              </div>
            </div>
          </div>
        </div>
        <div className="text-right flex-shrink-0">
          <div
            className={`text-xl font-bold ${
              isLinked ? "text-primary group-hover:text-primary/80" : "text-foreground"
            }`}
          >
            {formatCurrency(item.amount || 0, currency)}
          </div>
        </div>
      </div>
    );

    if (isLinked) {
      return (
        <Link key={item.id} href={`/account/subscriptions/${item.serviceId}`} className="block">
          {itemContent}
        </Link>
      );
    }

    return <div key={item.id}>{itemContent}</div>;
  };

  return (
    <div className="bg-card rounded-2xl border border-border overflow-hidden">
      <div className="px-6 py-4 bg-muted/50 border-b border-border">
        <div className="flex items-center justify-between">
          <h3 className="text-lg font-semibold text-foreground">Items & Services</h3>
          <div className="flex items-center gap-4 text-xs text-muted-foreground">
            {hasLinkedItems && (
              <div className="flex items-center gap-1.5">
                <div className="w-2 h-2 bg-success rounded-full"></div>
                <span>Linked to service</span>
              </div>
            )}
            {hasOneTimeItems && (
              <div className="flex items-center gap-1.5">
                <div className="w-2 h-2 bg-muted-foreground/50 rounded-full"></div>
                <span>One-time item</span>
              </div>
            )}
          </div>
        </div>
      </div>
      <div className="p-6">
        {items.length > 0 ? (
          <div className="space-y-3">{items.map(item => renderItemContent(item))}</div>
        ) : (
          <div className="text-center py-8">
            <div className="text-muted-foreground/50 mb-2">
              <svg
                className="w-12 h-12 mx-auto"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={1.5}
                  d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
                />
              </svg>
            </div>
            <p className="text-muted-foreground">No items found on this invoice.</p>
          </div>
        )}
      </div>
    </div>
  );
}

export type { InvoiceItemsProps };
