'use client';

import { useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useToast } from '@/components/crm/ToastProvider';
import { listSharedInboxes } from '@/features/inbox/api.client';
import type { SharedInboxList } from '@/features/inbox/types';
import { channelStatus, type ChannelCardId } from './data';

const NOT_AVAILABLE = [
  {
    name: 'WhatsApp',
    why: 'Needs a WhatsApp Business account connection, which the CRM does not have yet.',
    icon: 'M12 3a9 9 0 0 0-7.8 13.5L3 21l4.600-1.2A9 9 0 1 0 12 3Zm-3.2 5.2c.3 0 .6 0 .8.500l.8 2-.6.9c.8 1.600 2 2.800 3.600 3.600l.9-.7 2 .9c.3 1.100-.8 2.300-2 2.200-3.800-.4-7-3.600-7.400-7.400-.1-1 .9-2 1.900-2Z',
  },
  {
    name: 'Facebook Messenger',
    why: 'Needs a Facebook Page connection through Meta, which the CRM does not have yet.',
    icon: 'M12 3C7 3 3 6.700 3 11.400c0 2.600 1.300 4.900 3.300 6.400V21l3-1.700c.9.200 1.800.400 2.700.400 5 0 9-3.700 9-8.300S17 3 12 3Zm-5 11 3.200-4.800 2.600 2.300L17 9l-3.200 4.800-2.600-2.300L7 14Z',
  },
];

export function ChannelsSettingsView() {
  const toast = useToast();
  const router = useRouter();
  /** The team inboxes; null while unknown (or not mine to see). */
  const [inboxes, setInboxes] = useState<SharedInboxList | null>(null);
  useEffect(() => {
    listSharedInboxes().then(setInboxes, () => {});
  }, []);
  const status = channelStatus(inboxes?.data ?? []);
  const teamAddresses = inboxes ? inboxes.data.filter((i) => i.emailAddress).length : null;

  /* channels are switched on per team inbox, so that is where each card leads */
  const setUp = (card: ChannelCardId) => {
    if (card !== 'Team Email' && inboxes && !inboxes.canConnect) {
      toast('You need the “Connect channels” permission to set up channels.', 'info');
      return;
    }
    router.push('/settings/inboxes');
  };

  return (
    <>
      <main className="channels-main" id="channels-settings">
        <header className="set-head channels-page__head">
          <div>
            <h1 className="set-head__title">Channels</h1>
          </div>
        </header>

        <section className="channels-section" aria-labelledby="web-channels-title">
          <div className="channels-section__heading">
            <h2 id="web-channels-title">Web and apps channels</h2>
            <p>Connect chat and forms to engage visitors on your website and apps.</p>
          </div>

          <div className="channels-grid channels-grid--web">
            <article className="channel-card" data-channel-card="Chat">
              <button
                type="button"
                className="channel-card__link crm-d-flex crm-align-items-center crm-flex-column"
                onClick={() => setUp('Chat')}
              >
                <div className="channel-card__icon" aria-hidden="true">
                  <svg viewBox="0 0 24 24">
                    <path d="M7 5.5h12a1.5 1.5 0 0 1 1.5 1.5v9a1.5 1.5 0 0 1-1.5 1.5h-7l-4.5 3v-3H7A1.5 1.5 0 0 1 5.5 16V7A1.5 1.5 0 0 1 7 5.5Z" />
                    <path d="M3.5 14V5A1.5 1.5 0 0 1 5 3.5h12" />
                  </svg>
                </div>
                <h3>Chat</h3>
              </button>
              <button type="button" className="channel-connect-btn" onClick={() => setUp('Chat')}>
                {status.Chat ? 'Manage chat' : 'Connect chat'}
              </button>
              <p className="channel-card__status" aria-live="polite">
                {status.Chat}
              </p>
            </article>

            <article className="channel-card" data-channel-card="Form">
              <button
                type="button"
                className="channel-card__link crm-d-flex crm-align-items-center crm-flex-column"
                onClick={() => setUp('Form')}
              >
                <div className="channel-card__icon" aria-hidden="true">
                  <svg viewBox="0 0 24 24">
                    <rect x="3.5" y="3.5" width="17" height="4" rx="1" />
                    <rect x="3.5" y="10" width="17" height="4" rx="1" />
                    <rect x="3.5" y="16.5" width="17" height="4" rx="1" />
                  </svg>
                </div>
                <h3>Form</h3>
              </button>
              <button type="button" className="channel-connect-btn" onClick={() => setUp('Form')}>
                {status.Form ? 'Manage forms' : 'Connect form'}
              </button>
              <p className="channel-card__status" aria-live="polite">
                {status.Form}
              </p>
            </article>
          </div>
        </section>

        <section
          className="channels-section channels-section--team"
          aria-labelledby="team-channels-title"
        >
          <div className="channels-section__heading">
            <h2 id="team-channels-title">Team channels</h2>
            <p>Connect the channels your team uses to communicate with customers.</p>
          </div>

          <div className="channels-grid channels-grid--team">
            <article className="channel-card channel-card--email" data-channel-card="Team Email">
              <button
                type="button"
                className="channel-card__link crm-d-flex crm-align-items-center crm-flex-column"
                onClick={() => setUp('Team Email')}
              >
                <div className="channel-card__icon" aria-hidden="true">
                  <svg viewBox="0 0 24 24">
                    <rect x="2.8" y="5" width="18.4" height="14" rx="1.8" />
                    <path d="m4 7 8 6 8-6" />
                  </svg>
                </div>
                <h3>Team Email</h3>
              </button>
              <div className="channel-email-count">
                <button
                  type="button"
                  className="channel-email-count__link"
                  onClick={() => setUp('Team Email')}
                >
                  {teamAddresses === null
                    ? 'Team inboxes'
                    : `${teamAddresses} email ${teamAddresses === 1 ? 'address' : 'addresses'}`}
                </button>
              </div>
            </article>

            <article className="channel-card" data-channel-card="Calling">
              <button
                type="button"
                className="channel-card__link crm-d-flex crm-align-items-center crm-flex-column"
                onClick={() => setUp('Calling')}
              >
                <div className="channel-card__icon" aria-hidden="true">
                  <svg viewBox="0 0 24 24">
                    <path d="m7.2 3.8 3.1 3.1-2 2.5a14.1 14.1 0 0 0 6.3 6.3l2.5-2 3.1 3.1-1.8 3a2 2 0 0 1-2.2.9A19.4 19.4 0 0 1 3.2 7.9a2 2 0 0 1 .9-2.2l3.1-1.9Z" />
                  </svg>
                </div>
                <h3>Calling</h3>
              </button>
              <button
                type="button"
                className="channel-connect-btn channel-connect-btn--wide"
                onClick={() => setUp('Calling')}
              >
                {status.Calling ? 'Manage call logging' : 'Set up call logging'}
              </button>
              <p className="channel-card__status" aria-live="polite">
                {status.Calling}
              </p>
              <p className="channel-card__why">
                No phone system can be connected yet. Calls are written down in a team inbox.
              </p>
            </article>

            {/* listed as HubSpot lists them; neither can be connected yet, and they say so */}
            {NOT_AVAILABLE.map((channel) => (
              <article key={channel.name} className="channel-card" data-channel-card={channel.name}>
                <div className="channel-card__link crm-d-flex crm-align-items-center crm-flex-column">
                  <div className="channel-card__icon" aria-hidden="true">
                    <svg viewBox="0 0 24 24">
                      <path d={channel.icon} />
                    </svg>
                  </div>
                  <h3>{channel.name}</h3>
                </div>
                <button
                  type="button"
                  className="channel-connect-btn channel-connect-btn--wide"
                  disabled
                >
                  Not available yet
                </button>
                <p className="channel-card__why">{channel.why}</p>
              </article>
            ))}
          </div>
        </section>

        <section className="channels-migration" aria-labelledby="channels-migration-title">
          <h2 id="channels-migration-title">Move channels and conversation history</h2>
          <p>
            Move existing inbox channels and their historical conversations into help desk so your
            support team can manage ongoing and past customer conversations in one place. You can
            also move channels back to inbox if needed.
          </p>
          {/* there is no help desk to move into yet: the choices are shown, and say so (go-live QA) */}
          <div className="channels-migration__actions">
            <button
              type="button"
              className="channel-outline-action"
              data-migration="move"
              disabled
              aria-describedby="channels-migration-why"
            >
              <svg viewBox="0 0 16 16" aria-hidden="true">
                <rect x="3.5" y="7" width="9" height="7" rx="1" />
                <path d="M5.5 7V4.5a2.5 2.5 0 0 1 5 0V7M8 9.5v2" />
              </svg>
              Move channels to help desk
            </button>
            <button
              type="button"
              className="channel-outline-action"
              data-migration="revert"
              disabled
              aria-describedby="channels-migration-why"
            >
              Revert a channel back to inbox
            </button>
          </div>
          <p className="channels-migration__why" id="channels-migration-why">
            Not available yet: the CRM has no separate help desk. Your channels and their
            conversations stay in the Inbox.
          </p>
        </section>
      </main>
    </>
  );
}
