import type { Metadata } from "next";
import Link from "next/link";
import { 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: "Install | Monetize.com",
  description: "Install the Monetize.com code on your site.",
  path: "/hub/install",
  noindex: true,
});

export default async function HubInstall() {
  const user = await hubUser("/hub/install");
  return (
    <DbPage page="install" title="Install Monetize" crumb="Install" sub="Add one line of code to your site to start earning." data={{ sites: await hubSites(user.id) }} endpoints={{ check: "/api/hub/install-check" }}>
      <div className="db-split">
        <section className="db-card" aria-label="Installation steps">
          <ol className="db-steps">
            <li className="db-step">
              <div className="db-step__body">
                <h2 id="step1">Choose your site</h2>
                <div className="db-field">
                  <label htmlFor="db-install-site" className="db-sr">Site</label>
                  <select id="db-install-site" className="db-select" aria-labelledby="step1" defaultValue="">
                    <option value="">Select a site</option>
                  </select>
                </div>
                <p className="db-hint">Not listed? <Link href="/hub/sites">Add a site</Link> first.</p>
              </div>
            </li>
            <li className="db-step">
              <div className="db-step__body">
                <h2>Copy this Monetize code</h2>
                <div className="db-code">
                  <pre><code id="db-install-code"></code></pre>
                  <button type="button" id="db-copy-code" className="db-code__copy"><CopyIcon /><span>Copy</span></button>
                </div>
                <p id="db-key-note" className="db-msg db-msg--warn" hidden></p>
              </div>
            </li>
            <li className="db-step">
              <div className="db-step__body">
                <h2>Paste the code on your pages</h2>
                <p>Insert the code at the very end of each page, just before the closing <code>&lt;/body&gt;</code> tag.</p>
                <p>The code needs to be on every page where you want Monetize to work. The easiest way is to add it to your site&rsquo;s template.</p>
              </div>
            </li>
            <li className="db-step">
              <div className="db-step__body">
                <h2 id="step4">Check your installation</h2>
                <form id="db-check-form" className="db-field" noValidate>
                  <label htmlFor="db-check-url" className="db-sr">Your site URL</label>
                  <div className="db-inline">
                    <input type="text" id="db-check-url" className="db-input" placeholder="Your site URL" inputMode="url" autoComplete="url" aria-describedby="db-check-error" />
                    <button type="submit" className="db-btn db-btn--primary">Check</button>
                  </div>
                  <p id="db-check-error" className="db-error" role="alert"></p>
                </form>
                <p id="db-check-msg" className="db-msg" role="status" hidden></p>
              </div>
            </li>
          </ol>
        </section>

        <aside className="db-card db-help" aria-labelledby="help-title">
          <h2 id="help-title">Have a question?</h2>
          <p>Setup, compatibility and payouts are covered in the FAQ.</p>
          <Link className="db-btn db-btn--primary" href="/faq">Read our FAQ</Link>
          <a className="db-help__mail" href="mailto:support@monetize.com"><strong>Contact support</strong>support@monetize.com</a>
        </aside>
      </div>
    </DbPage>
  );
}
