'use client';

import type { FlightError } from '@/services/flights/errors';

interface FlightsErrorStateProps {
  /**
   * The structured failure. `message` is already written for a traveller —
   * upstream diagnostic text (Amadeus titles and the like) stays in `detail`
   * and is logged, never rendered here.
   */
  error?: FlightError | null;
  /**
   * Re-runs the same search that just failed. Ignored unless the error is
   * retryable — offering "Try Again" for a validation failure only invites the
   * user to repeat a request that cannot succeed.
   */
  onRetry?: () => void;
}

function SearchFailedIllustration() {
  return (
    <svg width={220} height={150} viewBox="0 0 220 150" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <ellipse cx={110} cy={128} rx={70} ry={8} fill="var(--color-first, #2f6271)" opacity={0.08} />

      <path d="M28 46c-5 0-9-4-9-9s4-9 9-9c1-6 6-10 12-10s11 4 12 10h2c6 0 10 4 10 9s-4 9-10 9H28Z" fill="var(--color-first, #2f6271)" opacity={0.12} />
      <path d="M168 88c-4 0-6-3-6-6s2-6 6-6c1-4 4-7 9-7s8 3 9 7h1c4 0 7 2 7 6s-3 6-7 6h-19Z" fill="var(--color-second, #f45d6b)" opacity={0.12} />

      {/* broken/interrupted flight path — dashes stop short, unlike the plain empty state's full path */}
      <path
        d="M36 108C60 78 100 64 130 56"
        stroke="var(--color-first, #2f6271)"
        strokeOpacity={0.35}
        strokeWidth={2}
        strokeDasharray="1 8"
        strokeLinecap="round"
        fill="none"
      />

      {/* plane, grounded/tilted rather than mid-flight */}
      <g transform="translate(118 44) rotate(75)">
        <path d="M0 14 30 0 6 28 3 18Z" fill="var(--color-first, #2f6271)" opacity={0.55} />
        <path d="M0 14 14 16 3 18Z" fill="var(--color-second, #f45d6b)" opacity={0.55} />
      </g>

      {/* warning marker where the path breaks off */}
      <circle cx={130} cy={56} r={13} stroke="var(--color-second, #f45d6b)" strokeWidth={2} fill="#fff" />
      <path d="M130 51v6M130 61h.01" stroke="var(--color-second, #f45d6b)" strokeWidth={2} strokeLinecap="round" />
    </svg>
  );
}

/**
 * Shown when the search itself failed (Amadeus/upstream error), as opposed
 * to FlightsEmptyState (search succeeded, genuinely zero matching flights).
 * These need different messaging — "try different dates" is actively wrong
 * advice when the real problem is a technical failure, not a lack of flights.
 */
export default function FlightsErrorState({ error, onRetry }: FlightsErrorStateProps) {
  const showRetry = Boolean(onRetry && (error?.retryable ?? true));

  return (
    <div
      className="flights-error-state bgColor-white rounded-3 text-center px-3"
      style={{ border: '1px solid #E5E7EB', paddingTop: '4rem', paddingBottom: '4rem' }}
    >
      <SearchFailedIllustration />
      <h4 className="text-xxl fw-600 color-text-dark mb-2 mt-4">We couldn&apos;t complete this search</h4>
      <p className="text-md flight-detail-label mb-0 mx-auto" style={{ maxWidth: 480, lineHeight: 1.6 }}>
        {error?.message || 'Something went wrong reaching our flight partner. Please try again in a moment.'}
      </p>
      {showRetry && (
        <button
          type="button"
          className="buttons btnColor-second text-center rounded-2 px-4 py-2 fw-normal text-sm mt-4 mx-auto cursor-pointer"
          onClick={onRetry}
        >
          Try Again
        </button>
      )}
    </div>
  );
}
