import React, { useContext, useEffect, useState } from 'react';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import styled from 'styled-components';
import ParticleBackground from '../../components/ParticleBackground/ParticleBackground';
import { AuthContext } from '../../contexts/AuthContext';
import { AnalyticsEvents, trackEvent } from '../../utils/analytics';

const PageContainer = styled.div`
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  color: #fff;
  font-family: 'Inter', sans-serif;
`;

const ContentWrapper = styled.div`
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 5;
  padding: 2rem;
`;

const GlassCard = styled.div`
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 1rem;
  padding: 3rem;
  width: 100%;
  max-width: 450px;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
`;

const CardTitle = styled.h2`
  font-size: 2rem;
  margin-bottom: 1rem;
  color: #fff;
`;

const BodyText = styled.p`
  color: rgba(255, 255, 255, 0.75);
  margin-bottom: 1.5rem;
  line-height: 1.5;
`;

const PrimaryButton = styled.button`
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 0.5rem;
  color: #fff;
  padding: 1rem 1.5rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  margin-bottom: 1rem;

  &:hover {
    transform: translateY(-2px);
  }
`;

const NavLink = styled(Link)`
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.9rem;
  text-decoration: none;

  &:hover {
    color: #fff;
    text-decoration: underline;
  }
`;

const BillingSuccess = (): JSX.Element => {
  const { authenticated, loading } = useContext(AuthContext);
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const [sessionId] = useState(() => searchParams.get('session_id') || '');

  useEffect(() => {
    trackEvent(AnalyticsEvents.PAYMENT_SUCCESS, {
      hasSessionId: Boolean(sessionId),
    });
  }, [sessionId]);

  useEffect(() => {
    if (!loading && authenticated) {
      const timer = window.setTimeout(() => navigate('/'), 2500);
      return () => window.clearTimeout(timer);
    }
  }, [authenticated, loading, navigate]);

  return (
    <PageContainer>
      <ParticleBackground />
      <ContentWrapper>
        <GlassCard>
          <CardTitle>Payment received</CardTitle>
          <BodyText>
            Thanks — your subscription checkout completed
            {sessionId ? ` (session ${sessionId.slice(0, 12)}…)` : ''}. You can continue into the app.
          </BodyText>
          {authenticated ? (
            <PrimaryButton type="button" onClick={() => navigate('/')}>
              Continue to app
            </PrimaryButton>
          ) : (
            <>
              <BodyText>Sign in with the account you just created to get started.</BodyText>
              <PrimaryButton type="button" onClick={() => navigate('/signin/')}>
                Sign in
              </PrimaryButton>
            </>
          )}
          <NavLink to="/signin/">Back to sign in</NavLink>
        </GlassCard>
      </ContentWrapper>
    </PageContainer>
  );
};

export default BillingSuccess;
