'use client';

import Link from 'next/link';
import { useEffect, useState } from 'react';
import { APP_NAME, CRM_ASSETS } from '@/config/constants';
import { getMe } from '@/features/auth/api.client';
import { SETUP_PERMISSIONS_PATH } from '@/features/extension/constants';

function firstName(full: string): string {
  return full.trim().split(/\s+/)[0] || 'there';
}

export function ExtensionCompleteView({ source }: { source?: 'login' }) {
  const [name, setName] = useState('there');
  const fromLogin = source === 'login';

  useEffect(() => {
    getMe()
      .then((me) => setName(firstName(me.user.name || me.user.email)))
      .catch(() => {});
  }, []);

  return (
    <main className="sales-setup-complete">
      {fromLogin ? (
        <header className="setup-topbar">
          <span className="setup-brand">{APP_NAME}</span>
        </header>
      ) : (
        <header className="setup-topbar" aria-label="Onboarding progress">
          <span className="setup-brand">{APP_NAME}</span>
          <div className="setup-progress" aria-label="Step 2 of 2">
            <span className="setup-progress__line" aria-hidden="true" />
            <div className="setup-progress__step is-complete">
              <span className="setup-progress__node">
                <svg viewBox="0 0 16 16" width="13" height="13" fill="none" aria-hidden>
                  <path
                    d="m3.2 8.2 3 3 6.5-6.4"
                    stroke="#fff"
                    strokeWidth="2"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  />
                </svg>
              </span>
              <span>Log into CRM</span>
            </div>
            <div className="setup-progress__step is-current">
              <span className="setup-progress__node" />
              <span>Explore features</span>
            </div>
          </div>
          <span className="setup-step-count">Step 2 of 2</span>
        </header>
      )}
      <div className="setup-main">
        <section className="setup-welcome" aria-labelledby="setup-welcome-title">
          <span className="setup-success-pill">
            <span className="setup-success-pill__icon" aria-hidden="true">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 20 20"
                width="12"
                height="12"
                fill="none"
              >
                <path
                  d="M4.5 10.2 8.1 13.5 15.5 6.5"
                  stroke="currentColor"
                  strokeWidth="2.4"
                  strokeLinecap="round"
                  strokeLinejoin="round"
                />
              </svg>
            </span>
            You&apos;re all set!
          </span>
          <h1 id="setup-welcome-title">
            Hi {name}!<span>You&apos;re all set.</span>
          </h1>
          <p className="setup-welcome__copy">
            You&apos;re signed in. Click the AdMedia CRM icon in Chrome to open your Activity Feed.
            Next, you can turn it on in Gmail.
          </p>
          <img
            className="setup-paper-plane"
            src={`${CRM_ASSETS}/img/plane.svg`}
            alt=""
            aria-hidden
          />
          <img
            className="setup-laptop-art"
            src={`${CRM_ASSETS}/img/all-set.svg`}
            alt="CRM Sales in the browser"
          />
        </section>
        <section className="setup-feature-list" aria-label="Explore extension features">
          <article className="setup-feature-card setup-feature-card--gmail">
            <img
              className="setup-feature-card__art"
              src={`${CRM_ASSETS}/img/gmail.svg`}
              alt=""
              aria-hidden
            />
            <div className="setup-feature-card__content">
              <div className="setup-feature-card__title-row">
                <h2>{APP_NAME} for Gmail</h2>
              </div>
              <p>
                Log sent mail onto contacts, track opens, and insert templates without leaving the
                inbox.
              </p>
              <Link
                className="setup-feature-card__action setup-feature-card__action--primary"
                href={SETUP_PERMISSIONS_PATH}
              >
                Start using in Gmail
              </Link>
            </div>
          </article>
          <article className="setup-feature-card setup-feature-card--everywhere">
            <img
              className="setup-feature-card__art"
              src={`${CRM_ASSETS}/img/everywhere.svg`}
              alt=""
              aria-hidden
            />
            <div className="setup-feature-card__content">
              <div className="setup-feature-card__title-row">
                <h2>Open the CRM</h2>
              </div>
              <p>Jump into contacts, companies, and deals in the full {APP_NAME} workspace.</p>
              <Link className="setup-feature-card__action" href="/home">
                Open workspace
              </Link>
            </div>
          </article>
        </section>
      </div>
    </main>
  );
}
