'use client';

import type { ActiveFilterChip, FlightFilters } from '@/services/flights/filterSort';
import type { FlightData } from './types';
import RelaxFilterChips from './RelaxFilterChips';
import './flight-filter-chips.css';

function FunnelIllustration() {
  return (
    <svg width={160} height={120} viewBox="0 0 160 120" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <ellipse cx={80} cy={104} rx={44} ry={6} fill="var(--color-first, #2f6271)" opacity={0.08} />
      {/* funnel */}
      <path
        d="M52 30h56L86 60v28l-12 8V60L52 30Z"
        stroke="var(--color-first, #2f6271)"
        strokeWidth={2.5}
        strokeLinejoin="round"
        fill="#fff"
      />
      <path d="M48 24h64" stroke="var(--color-first, #2f6271)" strokeWidth={2.5} strokeLinecap="round" />
      {/* nothing coming out */}
      <circle cx={80} cy={104} r={2.5} fill="var(--color-second, #f45d6b)" opacity={0.7} />
      <circle cx={92} cy={98} r={1.8} fill="var(--color-second, #f45d6b)" opacity={0.45} />
      <circle cx={69} cy={99} r={1.5} fill="var(--color-second, #f45d6b)" opacity={0.35} />
    </svg>
  );
}

interface FlightsFilteredEmptyStateProps {
  /** How many offers the search itself returned, before filtering. */
  totalBeforeFilters: number;
  /** The filters doing the excluding, each with the patch that clears it. */
  chips: ActiveFilterChip[];
  /** Apply one chip's `reset`. */
  onRemove: (chip: ActiveFilterChip) => void;
  onClearAll: () => void;
  /**
   * The unfiltered offers and active filters, for one-tap "Allow 1 stop ·
   * 34 results" widenings — optional, since the empty state is still
   * complete without them (just no relaxation chips).
   */
  offers?: FlightData[];
  filters?: FlightFilters;
  currency?: string;
  onRelax?: (patch: Partial<FlightFilters>) => void;
}

/**
 * Shown when the search found flights but the current filters exclude all of
 * them — a different situation from "this route has no flights", and one the
 * traveller can fix without touching the search.
 *
 * Rendered *beside* the filter sidebar, never instead of it. The earlier code
 * collapsed both cases into one `isEmpty` branch that replaced the whole row
 * with a full-width empty state, which took away the only control that could
 * undo what had just happened.
 *
 * The chips are the actual undo. "Clear all filters" is offered too, but it is
 * the blunt option — someone who set a price cap, a stop filter and a time
 * window, then went one step too far, wants to drop the one that went too far.
 */
export default function FlightsFilteredEmptyState({
  totalBeforeFilters,
  chips,
  onRemove,
  onClearAll,
  offers,
  filters,
  currency,
  onRelax,
}: FlightsFilteredEmptyStateProps) {
  return (
    <div
      className="flights-empty-state bgColor-white rounded-3 text-center px-3"
      style={{ border: '1px solid #E5E7EB', paddingTop: '3rem', paddingBottom: '3rem' }}
    >
      <FunnelIllustration />
      <h4 className="text-xxl fw-600 color-text-dark mb-2 mt-3">No flights match your filters</h4>
      <p className="text-md flight-detail-label mb-0 mx-auto" style={{ maxWidth: 480, lineHeight: 1.6 }}>
        {totalBeforeFilters > 0
          ? `This search found ${totalBeforeFilters} flight${totalBeforeFilters === 1 ? '' : 's'}, but none of them match everything you've selected. Remove a filter to see them.`
          : "None of the flights for this search match everything you've selected."}
      </p>

      {chips.length > 0 && (
        <div className="d-flex flex-wrap justify-content-center gap-2 mt-4">
          {chips.map((chip) => (
            <button
              key={chip.key}
              type="button"
              className="flights-filter-chip d-inline-flex align-items-center gap-2"
              onClick={() => onRemove(chip)}
              aria-label={`Remove filter: ${chip.label}`}
            >
              <span>{chip.label}</span>
              <svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
                <path d="m3 3 6 6M9 3l-6 6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
              </svg>
            </button>
          ))}
        </div>
      )}

      {chips.length > 0 && (
        <button
          type="button"
          className="flights-filter-clear-all mt-3"
          onClick={onClearAll}
        >
          Clear all filters
        </button>
      )}

      {offers && filters && onRelax ? (
        <RelaxFilterChips offers={offers} filters={filters} currency={currency ?? ''} onApply={onRelax} />
      ) : null}
    </div>
  );
}
