'use client';

import { useState } from 'react';
import { StepIcon } from './DynamicSequenceCanvas';
import { REP_KINDS, type RepKind } from './DynamicStepDrawers';

export type FirstStepKind = 'automated_email' | RepKind;

const isLinkedIn = (k: FirstStepKind) => k === 'linkedin_message' || k === 'linkedin_connect';

const CHOICES: Array<{ key: FirstStepKind; label: string; hint: string }> = [
  {
    key: 'automated_email',
    label: 'Automated email',
    hint: 'Automatically send an email for me',
  },
  ...REP_KINDS.map((k) => ({ key: k.key, label: k.label, hint: k.hint })),
];

/**
 * The step choices of a Step card (Step 1 and the "+" between steps). HubSpot (EDT-16): the
 * LinkedIn tasks sit behind a collapsed "LinkedIn Integration Tasks"; open, it asks to connect
 * LinkedIn Sales Navigator first. Without `onPick` they are only a preview (the create page).
 */
export function StepChoiceGrid({
  onPick,
  disabled,
  repOnly = false,
}: {
  onPick?: (kind: FirstStepKind) => void;
  disabled?: boolean;
  /** The rep lane of a dynamic sequence: no automated email. */
  repOnly?: boolean;
}) {
  const [linkedInOpen, setLinkedInOpen] = useState(false);
  const [connected, setConnected] = useState(false);
  const choice = (c: (typeof CHOICES)[number]) => {
    const body = (
      <>
        <StepIcon kind={c.key === 'automated_email' ? 'send' : c.key} />
        <span>
          <strong>{c.label}</strong>
          <small>{c.hint}</small>
        </span>
      </>
    );
    return onPick ? (
      <button
        key={c.key}
        type="button"
        className="dyn-choice"
        disabled={disabled}
        onClick={() => onPick(c.key)}
      >
        {body}
      </button>
    ) : (
      <div key={c.key} className="dyn-choice">
        {body}
      </div>
    );
  };
  return (
    <>
      <div className="seqc-step__grid">
        {CHOICES.filter((c) => !isLinkedIn(c.key) && !(repOnly && c.key === 'automated_email')).map(
          choice,
        )}
      </div>
      <div className="seqc-linkedin">
        <button
          type="button"
          className="seqc-linkedin__toggle"
          aria-expanded={linkedInOpen}
          onClick={() => setLinkedInOpen((open) => !open)}
        >
          <span aria-hidden>{linkedInOpen ? '⌃' : '⌄'}</span> LinkedIn Integration Tasks
        </button>
        {linkedInOpen ? (
          <>
            {connected ? (
              <div className="seqc-step__grid">
                {CHOICES.filter((c) => isLinkedIn(c.key)).map(choice)}
              </div>
            ) : (
              <div className="seqc-linkedin__connect">
                <span>
                  <strong>LinkedIn Sales Navigator</strong>
                  <small>Get task reminders to send InMail and connection requests</small>
                </span>
                <button
                  type="button"
                  className="btn-crm btn-crm--outline btn-crm--sm"
                  disabled={disabled}
                  onClick={() => setConnected(true)}
                >
                  Connect
                </button>
              </div>
            )}
            <button
              type="button"
              className="seqc-linkedin__toggle"
              onClick={() => setLinkedInOpen(false)}
            >
              <span aria-hidden>⌃</span> See less
            </button>
          </>
        ) : null}
      </div>
    </>
  );
}

/**
 * An empty standard sequence (design: "Step 1 / Choose the first action that the sequence will
 * perform"): the summary card, the six first steps and Completed. Without `onPick` it is only a
 * preview (the create page).
 */
export function SequenceStartPicker({
  onPick,
  disabled,
}: {
  onPick?: (kind: FirstStepKind) => void;
  disabled?: boolean;
}) {
  return (
    <div className="seqc-preview">
      <section className="dyn-stats" aria-label="Sequence summary">
        <div>
          <span>Total steps</span>
          <strong>0</strong>
        </div>
        <div>
          <span>Days to complete</span>
          <strong>1</strong>
        </div>
        <div>
          <span>Automated steps</span>
          <strong>0%</strong>
        </div>
        <div>
          <span>
            Unenroll criteria{' '}
            <span
              className="seqa-info"
              title="Contacts leave the sequence when they reply or book a meeting."
            >
              ⓘ
            </span>
          </span>
          <strong>2</strong>
        </div>
      </section>
      <span className="seqc-step__line" aria-hidden />
      <section className="seqc-step">
        <h3>Step 1</h3>
        <p>Choose the first action that the sequence will perform</p>
        <StepChoiceGrid onPick={onPick} disabled={disabled} />
      </section>
      <div className="seqc-step__done">
        <span className="seqc-step__line seqc-step__line--arrow" aria-hidden />
        <span className="dyn-done" aria-hidden>
          ✓
        </span>
        Completed
      </div>
    </div>
  );
}
