'use client';

import { useMemo, useState } from 'react';
import { CrmIcon } from '@/components/crm/CrmIcon';
import { useToast } from '@/components/crm/ToastProvider';
import { cn } from '@/lib/utils/cn';

type NvTab = 'business' | 'phone';

/** Number verification list stays empty until real CRM numbers are provisioned (HUB-416). */
const SAMPLE_NUMBERS: Array<{
  phone: string;
  country: string;
  assigned: string;
}> = [];

export function NumberVerificationView() {
  const toast = useToast();
  const [tab, setTab] = useState<NvTab>('business');
  const [search, setSearch] = useState('');
  const [countryOpen, setCountryOpen] = useState(false);
  const [assignedOpen, setAssignedOpen] = useState(false);
  const [countries, setCountries] = useState<string[]>([]);
  const [assigned, setAssigned] = useState<string[]>([]);

  const filtered = useMemo(() => {
    const q = search.trim().toLowerCase();
    return SAMPLE_NUMBERS.filter((row) => {
      if (q && !row.phone.toLowerCase().includes(q)) return false;
      if (countries.length && !countries.includes(row.country)) return false;
      if (assigned.length && !assigned.includes(row.assigned)) return false;
      return true;
    });
  }, [search, countries, assigned]);

  const filtersActive = countries.length > 0 || assigned.length > 0 || !!search.trim();

  return (
    <div className="calling-tool-page">
      <main className="calling-tool-main">
        <header className="set-head calling-tool-head">
          <h1 className="set-head__title">Number Verification</h1>
        </header>
        <p className="calling-tool-description">
          Number Verification gives U.S. numbers access to calling features that help improve answer
          rates, reduce the chance of calls being labeled as spam, and make it easier to gain
          compliance for phone numbers. By storing your compliance information as a business
          profile, and then verifying this identity and your use cases, you gain access to a variety
          of trust products that increase confidence and trust in your phone numbers.{' '}
          <a
            href="#"
            className="calling-tool-link"
            onClick={(e) => {
              e.preventDefault();
              toast('Learn more coming soon', 'info');
            }}
          >
            Learn more
            <CrmIcon name="external" className="iconSize-14" />
          </a>
        </p>

        <div className="calling-tool-tabs" role="tablist" aria-label="Number verification sections">
          {(
            [
              ['business', 'Business Profile'],
              ['phone', 'Verified Phone Numbers'],
            ] as const
          ).map(([id, label]) => (
            <button
              key={id}
              type="button"
              className={cn('calling-tool-tab', tab === id && 'is-active')}
              role="tab"
              aria-selected={tab === id}
              onClick={() => setTab(id)}
            >
              {label}
            </button>
          ))}
        </div>

        {tab === 'business' ? (
          <section className="calling-tool-panel" role="tabpanel">
            <h2>Business Profile</h2>
            <p>
              View an existing profile or submit one for verification. Once verified, the profile
              will be available across the rest of your Number Verification products.
            </p>
            <div className="calling-empty-card calling-empty-card--wide">
              <div className="calling-empty-card__copy">
                <h2>
                  Verify your U.S. business to improve call-answer rates and enhance trust in your
                  numbers!
                </h2>
                <p>
                  Take advantage of everything that Number Verification has to offer with a verified
                  business profile.
                </p>
                <button
                  type="button"
                  className="pp-btn pp-btn--primary"
                  onClick={() => toast('Create a Profile coming soon', 'info')}
                >
                  Create a Profile
                </button>
              </div>
              <div className="calling-empty-card__art" aria-hidden>
                <CrmIcon name="briefcase" className="iconSize-48" />
              </div>
            </div>
          </section>
        ) : (
          <section className="calling-tool-panel" role="tabpanel">
            <h2>Phone Numbers</h2>
            <p>
              These are the phone numbers that have been verified. Verify your numbers to gain
              access to a variety of trust related features.
            </p>
            <div className="calling-empty-card calling-empty-card--wide">
              <div className="calling-empty-card__copy">
                <h2>
                  Verify your U.S. business to improve call-answer rates and enhance trust in your
                  numbers!
                </h2>
                <p>
                  Take advantage of everything that Number Verification has to offer with a verified
                  business profile.
                </p>
                <button
                  type="button"
                  className="pp-btn pp-btn--primary"
                  onClick={() => toast('Create a Profile coming soon', 'info')}
                >
                  Create a Profile
                </button>
              </div>
              <div className="calling-empty-card__art" aria-hidden>
                <CrmIcon name="phone" className="iconSize-48" />
              </div>
            </div>

            <div className="ivr-table-wrap">
              <div className="cpt-toolbar">
                <div className="cpt-toolbar__left crm-px-2">
                  <div className="cpt-search-wrap">
                    <input
                      type="text"
                      className="cpt-search-input"
                      placeholder="Search phone numbers"
                      aria-label="Search phone numbers"
                      value={search}
                      onChange={(e) => setSearch(e.target.value)}
                    />
                    <span className="cpt-search-icon" aria-hidden>
                      <CrmIcon name="search" className="iconSize-16" />
                    </span>
                  </div>

                  <div className="cpt-filter-wrap">
                    <button
                      type="button"
                      className="cpt-filter-btn"
                      aria-expanded={countryOpen}
                      onClick={() => {
                        setCountryOpen((o) => !o);
                        setAssignedOpen(false);
                      }}
                    >
                      <span className="cpt-filter-label">
                        {countries.length ? `Country (${countries.length})` : 'Country'}
                      </span>
                      <svg
                        className="cpt-caret"
                        viewBox="0 0 10 6"
                        width="10"
                        height="6"
                        fill="currentColor"
                        aria-hidden
                      >
                        <path d="M0 0l5 5 5-5z" />
                      </svg>
                    </button>
                    {countryOpen ? (
                      <div className="cpt-filter-menu">
                        {['United States'].map((c) => (
                          <label className="cpt-option" key={c}>
                            <input
                              type="checkbox"
                              className="cpt-checkbox"
                              checked={countries.includes(c)}
                              onChange={() =>
                                setCountries((prev) =>
                                  prev.includes(c) ? prev.filter((x) => x !== c) : [...prev, c],
                                )
                              }
                            />
                            <span className="cpt-option-label">{c}</span>
                          </label>
                        ))}
                      </div>
                    ) : null}
                  </div>

                  <div className="cpt-filter-wrap">
                    <button
                      type="button"
                      className="cpt-filter-btn"
                      aria-expanded={assignedOpen}
                      onClick={() => {
                        setAssignedOpen((o) => !o);
                        setCountryOpen(false);
                      }}
                    >
                      <span className="cpt-filter-label">
                        {assigned.length ? `Assigned to (${assigned.length})` : 'Assigned to (0)'}
                      </span>
                      <svg
                        className="cpt-caret"
                        viewBox="0 0 10 6"
                        width="10"
                        height="6"
                        fill="currentColor"
                        aria-hidden
                      >
                        <path d="M0 0l5 5 5-5z" />
                      </svg>
                    </button>
                    {assignedOpen ? (
                      <div className="cpt-filter-menu">
                        {['Help desk', 'Inbox', 'Unassigned', 'User'].map((a) => (
                          <label className="cpt-option" key={a}>
                            <input
                              type="checkbox"
                              className="cpt-checkbox"
                              checked={assigned.includes(a)}
                              onChange={() =>
                                setAssigned((prev) =>
                                  prev.includes(a) ? prev.filter((x) => x !== a) : [...prev, a],
                                )
                              }
                            />
                            <span className="cpt-option-label">{a}</span>
                          </label>
                        ))}
                      </div>
                    ) : null}
                  </div>

                  {filtersActive ? (
                    <div className="cpt-results-wrap">
                      <span className="cpt-results-count">
                        Showing {filtered.length} of {SAMPLE_NUMBERS.length} numbers
                      </span>
                      <button
                        type="button"
                        className="cpt-clear-btn"
                        onClick={() => {
                          setSearch('');
                          setCountries([]);
                          setAssigned([]);
                        }}
                      >
                        Clear filters
                      </button>
                    </div>
                  ) : null}
                </div>
              </div>

              <div className="calling-phone-table-wrap">
                <table className="calling-phone-number-table">
                  <thead>
                    <tr>
                      <th className="cpt-th cpt-th--phone">
                        <div className="cpt-th-content">
                          <span className="cpt-th-title">Phone Number</span>
                          <span className="cpt-sort-icon">↑↓</span>
                        </div>
                      </th>
                      <th className="cpt-th">
                        <div className="cpt-th-content">
                          <span className="cpt-th-title">Connected To</span>
                        </div>
                      </th>
                      <th className="cpt-th">
                        <div className="cpt-th-content">
                          <span className="cpt-th-title">Caller ID</span>
                        </div>
                      </th>
                    </tr>
                  </thead>
                  <tbody>
                    {filtered.map((row) => (
                      <tr key={row.phone}>
                        <td>
                          <div className="cpt-phone-cell">
                            <span className="cpt-phone-val">{row.phone}</span>
                          </div>
                        </td>
                        <td>{row.assigned}</td>
                        <td>
                          <button
                            type="button"
                            className="cpt-verify-btn"
                            onClick={() => toast('Verification coming soon', 'info')}
                          >
                            Verify to turn on
                          </button>
                        </td>
                      </tr>
                    ))}
                    {!filtered.length ? (
                      <tr>
                        <td colSpan={3}>
                          <p className="calling-tool-description">No phone numbers match.</p>
                        </td>
                      </tr>
                    ) : null}
                  </tbody>
                </table>
              </div>
            </div>
          </section>
        )}
      </main>
    </div>
  );
}
