import type { Metadata } from "next";
import Link from "next/link";
import { CloseIcon, CopyIcon, DbPage } from "@/components/hub/DbPage";
import { hubSites, hubUser } from "@/lib/hub";
import { pageMetadata } from "@/lib/seo";

export const dynamic = "force-dynamic";

export const metadata: Metadata = pageMetadata({
  title: "Sites | Monetize.com",
  description: "Manage the sites that use Monetize.com.",
  path: "/hub/sites",
  noindex: true,
});

function DialogHead({ id, title, children }: { id: string; title: string; children?: React.ReactNode }) {
  return (
    <div className="db-dialog__head">
      {children ?? <h2 id={id}>{title}</h2>}
      <button type="button" className="db-iconbtn" data-close="" aria-label="Close"><CloseIcon /></button>
    </div>
  );
}

export default async function HubSites() {
  const user = await hubUser("/hub/sites");
  return (
    <DbPage page="sites" title="Sites" sub="Manage the sites and apps that use Monetize." data={{ sites: await hubSites(user.id) }} endpoints={{ sites: "/api/hub/sites" }}>
      <div className="db-split db-split--rev">
        <aside className="db-card db-disclosure" aria-labelledby="disc-title">
          <div className="db-card__head"><h2 id="disc-title">Disclosure</h2></div>
          <div className="db-card__body">
            <p>You may be legally required to tell your visitors that you use Monetize to earn revenue.</p>
            <Link href="/hub/disclosure">Find out more</Link>
          </div>
        </aside>

        <section className="db-card" aria-labelledby="sites-title">
          <div className="db-card__head">
            <div>
              <h2 id="sites-title">Your sites</h2>
              <p id="db-sites-count" className="db-merchants__meta"></p>
            </div>
            <button type="button" id="db-add-open" className="db-btn db-btn--primary"><svg className="db-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M5 12h14" /></svg>Add site</button>
          </div>
          <div className="db-table-wrap">
            <table className="db-table">
              <caption className="db-sr">Your sites</caption>
              <thead>
                <tr><th scope="col">Domain</th><th scope="col">Category</th><th scope="col">Type</th><th scope="col">Actions</th></tr>
              </thead>
              <tbody id="db-sites-body"></tbody>
            </table>
          </div>
        </section>
      </div>

      {/* Add site (4 steps) */}
      <dialog id="db-add-dialog" className="db-dialog" aria-labelledby="add-title">
        <form id="db-add-form" className="db-dialog__form" noValidate>
          <DialogHead id="add-title" title="Add a site">
            <div>
              <h2 id="add-title">Add a site</h2>
              <p id="db-add-step-label" className="db-hint">Step 1 of 4</p>
            </div>
          </DialogHead>
          <div className="db-dialog__body">
            <div className="db-wizard__step">
              <div className="db-field">
                <label htmlFor="db-add-url">Site URL, or extension/software download URL</label>
                <input type="text" id="db-add-url" className="db-input" inputMode="url" placeholder="mysite.com" aria-describedby="db-add-url-hint db-add-url-error" />
                <p id="db-add-url-hint" className="db-hint">For example mysite.com or example.com/blog. You can add more sites later.</p>
                <p id="db-add-url-error" className="db-error" role="alert"></p>
              </div>
            </div>
            <div className="db-wizard__step" hidden>
              <fieldset className="db-fieldset">
                <legend className="db-legend"><h3>Where will you promote affiliate links?</h3></legend>
                <p className="db-hint">Choose all that apply. This helps match you with as many merchants as possible.</p>
                <div id="db-add-types" className="db-choices"></div>
              </fieldset>
              <p id="db-add-types-error" className="db-error" role="alert"></p>
            </div>
            <div className="db-wizard__step" hidden>
              <fieldset className="db-fieldset">
                <legend className="db-legend"><h3>What is the main type of content on your site?</h3></legend>
                <p className="db-hint">Choose one.</p>
                <div id="db-add-category" className="db-choices"></div>
              </fieldset>
              <p id="db-add-category-error" className="db-error" role="alert"></p>
            </div>
            <div className="db-wizard__step" hidden>
              <div className="db-done">
                <span className="db-done__icon"><svg className="db-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m5 12 5 5L20 7" /></svg></span>
                <h3>Site added</h3>
                <p className="db-hint"><strong id="db-add-done-site"></strong> is in your site list. Next, <Link href="/hub/install">install the Monetize code</Link>.</p>
                <p id="db-add-done-note" className="db-msg db-msg--warn" hidden>Preview only: this site isn&rsquo;t saved to your account yet.</p>
              </div>
            </div>
          </div>
          <div className="db-dialog__foot">
            <div className="db-dots" aria-hidden="true"><span aria-current="step"></span><span></span><span></span><span></span></div>
            <button type="button" id="db-add-back" className="db-btn db-btn--ghost" hidden>Back</button>
            <button type="button" id="db-add-next" className="db-btn db-btn--primary">Next</button>
          </div>
        </form>
      </dialog>

      {/* API key */}
      <dialog id="db-key-dialog" className="db-dialog db-dialog--sm" aria-labelledby="key-title">
        <div className="db-dialog__form">
          <DialogHead id="key-title" title="API keys" />
          <div className="db-dialog__body">
            <dl className="db-keys">
              <div><dt>Site</dt><dd id="db-key-site"></dd></div>
              <div>
                <dt>API key</dt>
                <dd className="db-key"><code id="db-key-value"></code></dd>
              </div>
            </dl>
          </div>
          <div className="db-dialog__foot">
            <button type="button" id="db-key-copy" className="db-btn db-btn--ghost"><CopyIcon /><span>Copy key</span></button>
            <a id="db-key-install" className="db-btn db-btn--primary" href="/hub/install"><svg className="db-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m8 7-5 5 5 5M16 7l5 5-5 5" /></svg>Get install code</a>
          </div>
        </div>
      </dialog>

      {/* Edit site */}
      <dialog id="db-edit-dialog" className="db-dialog" aria-labelledby="edit-title">
        <form id="db-edit-form" className="db-dialog__form" noValidate>
          <DialogHead id="edit-title" title="Update site" />
          <div className="db-dialog__body">
            <div className="db-field">
              <label htmlFor="db-edit-domain">Domain</label>
              <input type="text" id="db-edit-domain" className="db-input" inputMode="url" />
            </div>
            <div className="db-field">
              <label htmlFor="db-edit-category">Category</label>
              <select id="db-edit-category" className="db-select"></select>
            </div>
            <fieldset className="db-fieldset">
              <legend className="db-legend">Type</legend>
              <div id="db-edit-types" className="db-choices"></div>
            </fieldset>
            <p id="db-edit-error" className="db-error" role="alert"></p>
          </div>
          <div className="db-dialog__foot">
            <button type="button" className="db-btn db-btn--ghost" data-close="">Cancel</button>
            <button type="submit" className="db-btn db-btn--primary">Update site</button>
          </div>
        </form>
      </dialog>

      {/* Delete site */}
      <dialog id="db-delete-dialog" className="db-dialog db-dialog--sm" aria-labelledby="del-title">
        <div className="db-dialog__form">
          <DialogHead id="del-title" title="Delete site?" />
          <div className="db-dialog__body">
            <p><strong id="db-delete-site"></strong> will be removed from your account and Monetize will stop working on it.</p>
          </div>
          <div className="db-dialog__foot">
            <button type="button" className="db-btn db-btn--ghost" data-close="">Cancel</button>
            <button type="button" id="db-delete-confirm" className="db-btn db-btn--danger">Delete site</button>
          </div>
        </div>
      </dialog>
    </DbPage>
  );
}
