import React, { useMemo } from 'react';
import { History } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';

function hasHistoricalVisibility(bill) {
  const visibility = bill.history_visibility;
  return !!bill.has_history_ranges || (visibility && visibility !== 'default');
}

export const MobileBillRow = React.memo(function MobileBillRow({ bill, onEdit, onToggle, onDelete, onHistory }) {
  const hasHistory = useMemo(() => hasHistoricalVisibility(bill), [bill]);
  
  const statusClass = useMemo(() => {
    return cn(
      'rounded px-1.5 py-0.5 text-[10px] font-semibold uppercase tracking-wide',
      bill.active
        ? 'bg-emerald-500/15 text-emerald-500'
        : 'bg-muted text-muted-foreground',
    );
  }, [bill.active]);

  const autopayClass = useMemo(() => {
    return cn(
      'rounded bg-emerald-500/20 px-1.5 py-0.5 text-[10px] font-semibold text-emerald-500',
      !!bill.autopay_enabled ? 'opacity-100' : 'opacity-0',
    );
  }, [bill.autopay_enabled]);

  const toggleBtnClass = useMemo(() => {
    return cn(
      'h-8 px-2.5 text-xs',
      bill.active
        ? 'text-muted-foreground hover:text-destructive'
        : 'text-emerald-500 hover:text-emerald-400',
    );
  }, [bill.active]);

  return (
    <div className="rounded-lg border border-border/60 bg-background/60 p-3 shadow-sm">
      <div className="flex min-w-0 items-start justify-between gap-3">
        <div className="min-w-0">
          <div className="flex min-w-0 items-center gap-2">
            <button
              type="button"
              className="min-w-0 truncate text-left text-sm font-semibold leading-tight text-foreground underline-offset-4 transition-colors hover:text-primary hover:underline focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 rounded-sm"
              onClick={() => onEdit?.(bill.id)}
              title={`Edit ${bill.name}`}
            >
              {bill.name}
            </button>
            {hasHistory && (
              <span
                className="inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-full border border-sky-500/25 bg-sky-500/10 text-sky-500"
                title="Historical visibility configured"
                aria-label="Historical visibility configured"
              >
                <History className="h-3 w-3" />
              </span>
            )}
          </div>

          <div className="mt-1 flex flex-wrap items-center gap-1.5">
            <span className={statusClass}>
              {bill.active ? 'Active' : 'Inactive'}
            </span>
            {bill.autopay_enabled && (
              <span className="rounded bg-emerald-500/20 px-1.5 py-0.5 text-[10px] font-semibold text-emerald-500">AP</span>
            )}
            {bill.has_2fa && (
              <span className="rounded bg-violet-500/15 px-1.5 py-0.5 text-[10px] text-violet-400">2FA</span>
            )}
          </div>
        </div>

        <span className="shrink-0 font-mono text-sm font-semibold tabular-nums text-foreground">
          ${Number(bill.expected_amount).toFixed(2)}
        </span>
      </div>

      <div className="mt-3 grid grid-cols-3 gap-2 text-xs text-muted-foreground">
        <div>
          <p className="uppercase tracking-wide text-muted-foreground/60">Due</p>
          <p className="mt-0.5 text-sm text-foreground">Day {bill.due_day}</p>
        </div>
        <div className="min-w-0">
          <p className="uppercase tracking-wide text-muted-foreground/60">Category</p>
          <p className="mt-0.5 truncate text-sm text-foreground">{bill.category_name || '—'}</p>
        </div>
        <div>
          <p className="uppercase tracking-wide text-muted-foreground/60">Cycle</p>
          <p className="mt-0.5 text-sm capitalize text-foreground">{bill.billing_cycle || 'monthly'}</p>
        </div>
      </div>

      <div className="mt-3 flex flex-wrap items-center justify-end gap-1.5">
        <Button
          variant="ghost"
          size="sm"
          className={toggleBtnClass}
          onClick={() => onToggle?.(bill)}
        >
          {bill.active ? 'Deactivate' : 'Activate'}
        </Button>
        {!bill.active && (
          <Button
            variant="ghost"
            size="sm"
            className="h-8 px-2.5 text-xs text-sky-500 hover:text-sky-400 hover:bg-sky-500/10"
            onClick={() => onHistory?.(bill)}
          >
            History
          </Button>
        )}
        <Button
          variant="ghost"
          size="sm"
          className="h-8 px-2.5 text-xs text-destructive hover:text-destructive hover:bg-destructive/10"
          onClick={() => onDelete?.(bill)}
        >
          Delete
        </Button>
      </div>
    </div>
  );
});

MobileBillRow.displayName = 'MobileBillRow';
