'use client';

import { useEffect, useRef, useState } from 'react';

import { GlassBanner } from '@/components/GlassBanner/GlassBanner';
import { ButtonVariant } from '@/components/button/Button';
import CloseButton from '@/components/button/CloseButton';
import { components } from '@/lib/gen';
import logWebUserEvent from '@/logging/logWebUserEvent';

type EligibleDiscountsResponse =
  components['schemas']['EligibleDiscountsResponse'];

const HARVARD_BANNER_DISMISSED_KEY = 'harvard-banner-dismissed';

/**
 * Checks if user has the Harvard discount (slug: "harvard2025")
 */
export const shouldShowHarvardBanner = (
  eligibleDiscountsResult: EligibleDiscountsResponse | undefined
): boolean => {
  if (!eligibleDiscountsResult?.eligible_discounts) {
    return false;
  }

  // Check all plan keys and periods for harvard2025 slug
  for (const planDiscounts of Object.values(
    eligibleDiscountsResult.eligible_discounts
  )) {
    if (planDiscounts) {
      for (const discountDetails of Object.values(planDiscounts)) {
        if (discountDetails?.slug === 'harvard2025') {
          return true;
        }
      }
    }
  }

  return false;
};

export const HarvardBanner = () => {
  const [isDismissed, setIsDismissed] = useState(false);
  const hasLoggedShownRef = useRef(false);

  // Read from localStorage after mount to check if dismissed
  useEffect(() => {
    const wasDismissed =
      localStorage.getItem(HARVARD_BANNER_DISMISSED_KEY) === 'true';
    setIsDismissed(wasDismissed);
  }, []);

  // Log when banner is shown (only once and only if not dismissed)
  useEffect(() => {
    if (!isDismissed && !hasLoggedShownRef.current) {
      logWebUserEvent({
        actionName: 'SchoolBannerShown',
        context: {
          schoolName: 'harvard',
        },
      });
      hasLoggedShownRef.current = true;
    }
  }, [isDismissed]);

  if (isDismissed) {
    return null;
  }

  const handleDismiss = () => {
    logWebUserEvent({
      actionName: 'SchoolBannerDismissed',
      context: {
        schoolName: 'harvard',
      },
    });
    localStorage.setItem(HARVARD_BANNER_DISMISSED_KEY, 'true');
    setIsDismissed(true);
  };

  return (
    <GlassBanner className='border border-accent-brand/30 bg-accent-brand/10'>
      <div className='flex flex-col gap-3'>
        <div className='flex items-start justify-between gap-4'>
          <div className='flex flex-col gap-1'>
            <h2 className='font-sans text-lg font-bold text-accent-brand'>
              Suno for Harvard Students
            </h2>
            <p className='font-sans text-sm text-accent-brand/80'>
              From us to you: select 'Monthly' to unlock your discount and
              kickstart your creation
            </p>
          </div>
          <CloseButton
            onClick={handleDismiss}
            aria-label='Dismiss banner'
            variant={ButtonVariant.LightGlass}
          />
        </div>
        <p className='font-sans text-sm leading-relaxed text-foreground-primary'>
          Whether you're coding in Widener, writing at 3am in your dorm, or
          dreaming up something new in a practice room—your ideas deserve to be
          heard. With Suno, those ideas can become music. Built by fellow
          Harvard alumni.
        </p>
      </div>
    </GlassBanner>
  );
};
