// PitStop UI Kit — Auth + Dashboard screens
// Recreated from apps/web/src/pages/Auth.tsx and Index.tsx

// ── Shared top strip (dashboard) ──────────────────────────────────────────
function DashTopBar({ initials, unread = 0, onOpenNotifications }) {
  return (
    <div style={{ position: 'sticky', top: 0, zIndex: 10, background: 'hsl(var(--bg) / 0.82)',
      backdropFilter: 'blur(12px)', borderBottom: '1px solid hsl(var(--line))', padding: '56px 16px 12px' }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, minWidth: 0 }}>
          <DotsLogo width={36} height={18} />
          <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 18,
            color: 'hsl(var(--ink))', letterSpacing: '-0.02em' }}>PitStop</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <NotificationBell count={unread} onOpen={onOpenNotifications} />
          <ProfileButton initials={initials} />
        </div>
      </div>
    </div>
  );
}

// ── Auth screen ───────────────────────────────────────────────────────────
function AuthScreen({ onSignIn }) {
  useLucide();
  const [email, setEmail] = useState('alex@pitstop.app');
  const [pw, setPw] = useState('••••••••');
  const field = (icon, props) => (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10, background: 'hsl(var(--paper))',
      border: '1px solid hsl(var(--border))', borderRadius: 'var(--radius-md)', padding: '0 14px', height: 46 }}>
      <Icon name={icon} size={16} color="hsl(var(--muted-ink))" />
      <input {...props} style={{ border: 'none', outline: 'none', background: 'transparent', flex: 1,
        fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 15, color: 'hsl(var(--ink))' }} />
    </div>
  );
  return (
    <div className="bg-dot-grid" style={{ minHeight: '100%', display: 'flex', alignItems: 'center',
      justifyContent: 'center', padding: 24, boxSizing: 'border-box', background: 'hsl(var(--bg))' }}>
      <Card style={{ width: '100%', maxWidth: 340, padding: '32px 24px 26px', boxShadow: 'var(--shadow-card)' }}>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8, marginBottom: 22 }}>
          <div style={{ width: 56, height: 56, borderRadius: 16, background: 'hsl(var(--ember))',
            display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <DotsLogo width={34} height={17} style={{ filter: 'brightness(0) invert(1)' }} />
          </div>
          <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 22, color: 'hsl(var(--ink))', letterSpacing: '-0.02em' }}>PitStop</span>
          <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 500, fontSize: 13, color: 'hsl(var(--muted-ink))' }}>Sign in to plan your trips</span>
        </div>

        <button onClick={onSignIn} style={{ width: '100%', height: 46, borderRadius: 999, background: 'transparent',
          border: '2px solid hsl(var(--border))', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, whiteSpace: 'nowrap',
          fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 14, color: 'hsl(var(--ink))', cursor: 'pointer', marginBottom: 18 }}>
          <svg width="16" height="16" viewBox="0 0 24 24"><path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"/><path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>
          Continue with Google
        </button>

        <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 18px' }}>
          <div style={{ flex: 1, height: 1, background: 'hsl(var(--border))' }} />
          <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 11, color: 'hsl(var(--muted-ink))', textTransform: 'uppercase' }}>or</span>
          <div style={{ flex: 1, height: 1, background: 'hsl(var(--border))' }} />
        </div>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
          {field('mail', { value: email, onChange: e => setEmail(e.target.value) })}
          {field('lock', { value: pw, type: 'text', onChange: e => setPw(e.target.value) })}
          <div style={{ textAlign: 'right', marginTop: -4 }}>
            <span style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'hsl(var(--muted-ink))', cursor: 'pointer' }}>Forgot password?</span>
          </div>
          <Btn full onClick={onSignIn} size="md" style={{ height: 46 }}>Sign In</Btn>
        </div>
        <div style={{ textAlign: 'center', marginTop: 18, fontFamily: 'var(--font-sans)', fontSize: 13, color: 'hsl(var(--muted-ink))' }}>
          Don't have an account? <span style={{ color: 'hsl(var(--ember))', fontWeight: 700, cursor: 'pointer' }}>Sign up</span>
        </div>
      </Card>
    </div>
  );
}

function PostTripBalance({ provisional = false, multiple = false, onOpen }) {
  return (
    <div style={{ border: '1px solid var(--line)', borderRadius: 14, padding: 14, background: 'hsl(var(--paper))' }}>
      <Kicker color={provisional ? undefined : 'hsl(var(--ember))'}>{provisional ? 'Not settled up yet' : 'Awaiting payment'}</Kicker>
      <div style={{ marginTop: 6, fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 16, color: 'hsl(var(--ink))' }}>
        {multiple ? '3 requests · 2 periods' : provisional ? 'You are owed €50.00' : 'Mara owes you €40.00'}
      </div>
      <div style={{ marginTop: 5, fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 12, color: 'hsl(var(--muted-ink))' }}>
        {multiple ? 'EUR and GBP shown separately' : provisional ? 'Current estimate · may change' : '20–21 Apr · EUR · Payee: you'}
      </div>
      <Btn variant="outline" full onClick={onOpen} style={{ marginTop: 12 }}>{provisional ? 'View tab' : multiple ? 'View payments' : 'View payment'}</Btn>
    </div>
  );
}

// ── Trip card (rich, upcoming) ────────────────────────────────────────────
function TripCardRich({ trip, onClick }) {
  return (
    <Card hoverable onClick={onClick} style={{ padding: 16 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <Kicker style={{ letterSpacing: '0.08em' }}>{`Trip · ${trip.days} Days`}</Kicker>
        {trip.badge && <Kicker color={trip.badgeAccent ? 'hsl(var(--ember))' : undefined} style={{ letterSpacing: '0.08em' }}>{trip.badge}</Kicker>}
      </div>
      <div style={{ margin: '12px 0' }}><RouteTimeline count={trip.stops} /></div>
      <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 19, color: 'hsl(var(--ink))', letterSpacing: '-0.02em' }}>{trip.name}</div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 8 }}>
        <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 11, textTransform: 'uppercase', letterSpacing: '0.06em', color: 'hsl(var(--muted-ink))' }}>{trip.range}</span>
        <AvatarStack people={trip.people} />
      </div>
    </Card>
  );
}

function TripCardPast({ trip, onClick }) {
  return (
    <Card hoverable onClick={onClick} style={{ padding: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
      <div style={{ width: 48, display: 'flex', justifyContent: 'center' }}>
        <span style={{ display: 'flex', gap: 5 }}>
          {[0, 1, 2].map(i => <span key={i} style={{ width: 7, height: 7, borderRadius: 999,
            background: i === 2 ? 'hsl(var(--ember))' : 'hsl(var(--ink) / 0.5)' }} />)}
        </span>
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 16, color: 'hsl(var(--ink))' }}>{trip.name}</div>
        <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 11, textTransform: 'uppercase',
          letterSpacing: '0.06em', color: 'hsl(var(--muted-ink))', marginTop: 2 }}>{`Trip · ${trip.range}`}</div>
      </div>
      <Icon name="chevron-right" size={20} color="hsl(var(--muted-ink))" />
    </Card>
  );
}

// ── Activity card (compact row) ───────────────────────────────────────────
function ActivityCardRow({ act, onClick }) {
  const glyph = act.type === 'flight'
    ? <Icon name="plane" size={20} color="hsl(var(--muted-ink))" />
    : act.type === 'note'
      ? <Icon name="sticky-note" size={20} color="hsl(var(--muted-ink))" />
      : <HopArc width={48} height={16} />;
  return (
    <Card hoverable onClick={onClick} style={{ padding: 16, display: 'flex', alignItems: 'center', gap: 12 }}>
      <div style={{ width: 48, flexShrink: 0, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{glyph}</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.08em', color: 'hsl(var(--ember))' }}>{`Activity · ${act.date}`}</div>
        <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 16, color: 'hsl(var(--ink))', marginTop: 2 }}>{act.title}</div>
        <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 11, textTransform: 'uppercase', letterSpacing: '0.06em', color: 'hsl(var(--muted-ink))', marginTop: 2 }}>{act.meta}</div>
      </div>
      <Icon name="chevron-right" size={20} color="hsl(var(--muted-ink))" />
    </Card>
  );
}

// ── Dashboard ─────────────────────────────────────────────────────────────
function DashboardScreen({ data, onOpenTrip }) {
  useLucide();
  const [filter, setFilter] = useState('all');
  const [upcoming, setUpcoming] = useState(false);
  const inbox = useNotificationInbox();
  return (
    <div style={{ minHeight: '100%', background: 'hsl(var(--bg))' }}>
      <DashTopBar initials="AB" unread={inbox.unread} onOpenNotifications={inbox.openSheet} />
      {inbox.sheet}
      <div style={{ padding: '16px 16px 120px', display: 'flex', flexDirection: 'column', gap: 22 }}>
        <div>
          <Kicker>Your trips &amp; activities</Kicker>
          <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 26, color: 'hsl(var(--ink))', letterSpacing: '-0.02em', marginTop: 4 }}>Where to next?</div>
        </div>

        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <div style={{ display: 'flex', gap: 6 }}>
            <Chip active={filter === 'all'} onClick={() => setFilter('all')}>All</Chip>
            <Chip active={filter === 'trips'} onClick={() => setFilter('trips')}>Trips</Chip>
            <Chip active={filter === 'activities'} onClick={() => setFilter('activities')}>Activities</Chip>
          </div>
          <Chip active={upcoming} accent onClick={() => setUpcoming(v => !v)}>Upcoming</Chip>
        </div>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
          {filter !== 'activities' && <TripCardRich trip={data.trips[0]} onClick={() => onOpenTrip(data.trips[0])} />}
          {filter !== 'trips' && <ActivityCardRow act={data.activities[0]} />}
          {filter !== 'activities' && <TripCardRich trip={data.trips[1]} onClick={() => onOpenTrip(data.trips[1])} />}
          {filter !== 'trips' && <ActivityCardRow act={data.activities[1]} />}

          {!upcoming && (
            <>
              <Kicker style={{ padding: '4px 4px 0', letterSpacing: '0.08em' }}>Past</Kicker>
              <TripCardPast trip={data.trips[2]} onClick={() => onOpenTrip(data.trips[2])} />
              <PostTripBalance onOpen={() => onOpenTrip(data.trips[2])} />
            </>
          )}
        </div>
      </div>

      {/* Floating create actions */}
      <div style={{ position: 'absolute', bottom: 28, right: 16, display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 12 }}>
        <Btn variant="outline" size="sm" style={{ height: 44, background: 'hsl(var(--bg) / 0.9)', backdropFilter: 'blur(8px)', boxShadow: 'var(--shadow-soft)', textTransform: 'none', letterSpacing: 0, fontWeight: 700, fontSize: 14 }}>
          <HopArc width={24} height={12} /> New activity
        </Btn>
        <Btn size="md" style={{ height: 48, textTransform: 'none', letterSpacing: 0, fontWeight: 700, fontSize: 14 }}>
          <Icon name="plus" size={20} color="#fff" /> New trip
        </Btn>
      </div>
    </div>
  );
}

Object.assign(window, { AuthScreen, DashboardScreen, TripCardRich, TripCardPast, ActivityCardRow, DashTopBar, PostTripBalance });
