import React from "react";

const ADMIN_URLS = {
  settings: "admin.php?page=pointlybooking_settings",
  payments: "admin.php?page=pointlybooking_settings&tab=payments",
  schedule: "admin.php?page=pointlybooking_settings&tab=schedule",
  automation: "admin.php?page=pointlybooking_settings&tab=notifications",
  services: "admin.php?page=pointlybooking_services",
  agents: "admin.php?page=pointlybooking_agents",
  calendar: "admin.php?page=pointlybooking_calendar",
  wizard: "admin.php?page=pointlybooking_design_form",
  formFields: "admin.php?page=pointlybooking_settings&tab=form_fields",
  coupons: "admin.php?page=pointlybooking_settings&tab=promo_codes",
};

function Code({ children }) {
  return (
    <pre
      style={{
        background: "#0b1220",
        color: "#e5e7eb",
        padding: "12px 14px",
        borderRadius: 12,
        overflow: "auto",
        margin: "10px 0 0",
        fontSize: 13,
        lineHeight: 1.45,
      }}
    >
      <code>{children}</code>
    </pre>
  );
}

function ActionLink({ href, children, primary = false }) {
  return (
    <a className={`bp-btn ${primary ? "bp-btn-primary" : ""}`.trim()} href={href}>
      {children}
    </a>
  );
}

function GuideCard({ title, children, actions = null }) {
  return (
    <section className="bp-card" style={{ padding: 18 }}>
      <div className="bp-section-title" style={{ margin: 0, fontSize: 16 }}>
        {title}
      </div>
      <div className="bp-muted" style={{ marginTop: 10, lineHeight: 1.7 }}>
        {children}
      </div>
      {actions ? (
        <div style={{ display: "flex", gap: 10, flexWrap: "wrap", marginTop: 14 }}>
          {actions}
        </div>
      ) : null}
    </section>
  );
}

export default function HowToUseScreen() {
  return (
    <div className="myplugin-page bp-howto">
      <main className="myplugin-content">
        <div className="bp-page-head">
          <div>
            <div className="bp-h1">How to Use BookPoint</div>
            <div className="bp-muted">
              Follow a clean setup path, publish the booking wizard, and learn the main admin flow fast.
            </div>
          </div>
          <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
            <ActionLink href={ADMIN_URLS.settings} primary>Open Settings</ActionLink>
            <ActionLink href={ADMIN_URLS.wizard}>Open Booking Form</ActionLink>
          </div>
        </div>

        <section className="bp-card" style={{ padding: 18, marginBottom: 14 }}>
          <div
            style={{
              display: "grid",
              gridTemplateColumns: "repeat(auto-fit, minmax(280px, 1fr))",
              gap: 16,
              alignItems: "start",
            }}
          >
            <div>
              <div className="bp-card-label">Start Here</div>
              <div style={{ marginTop: 6, fontSize: 22, lineHeight: 1.2, fontWeight: 900, color: "#0f172a" }}>
                Set up BookPoint and launch the booking wizard without guesswork.
              </div>
              <div className="bp-muted" style={{ marginTop: 10, lineHeight: 1.7 }}>
                Best order for a new site: settings, services, agents, schedule, booking form, then publish the shortcode
                on a page.
              </div>
              <div style={{ display: "flex", gap: 10, flexWrap: "wrap", marginTop: 14 }}>
                <ActionLink href={ADMIN_URLS.services}>Create Services</ActionLink>
                <ActionLink href={ADMIN_URLS.agents}>Create Agents</ActionLink>
                <ActionLink href={ADMIN_URLS.schedule}>Set Schedule</ActionLink>
              </div>
            </div>

            <div>
              <div className="bp-card-label">Important Pages</div>
              <div className="bp-quick-actions" style={{ marginTop: 10 }}>
                <a className="bp-quick-link" href={ADMIN_URLS.settings}>Global settings</a>
                <a className="bp-quick-link" href={ADMIN_URLS.wizard}>Booking wizard setup</a>
                <a className="bp-quick-link" href={ADMIN_URLS.formFields}>Custom form fields</a>
                <a className="bp-quick-link" href={ADMIN_URLS.automation}>Automation and reminders</a>
                <a className="bp-quick-link" href={ADMIN_URLS.payments}>Payments</a>
                <a className="bp-quick-link" href={ADMIN_URLS.calendar}>Calendar</a>
              </div>
            </div>
          </div>
        </section>

        <div className="bp-grid" style={{ gridTemplateColumns: "repeat(auto-fit, minmax(320px, 1fr))", gap: 14 }}>
          <GuideCard
            title="1) Recommended setup order"
            actions={[
              <ActionLink key="settings" href={ADMIN_URLS.settings}>Settings</ActionLink>,
              <ActionLink key="services" href={ADMIN_URLS.services}>Services</ActionLink>,
              <ActionLink key="agents" href={ADMIN_URLS.agents}>Agents</ActionLink>,
            ]}
          >
            <ol style={{ margin: 0, paddingLeft: 18 }}>
              <li>Open <b>BookPoint -&gt; Settings</b> and save your defaults first.</li>
              <li>Create your <b>Services</b> and organize them if needed.</li>
              <li>Create your <b>Agents</b> and assign services to them.</li>
              <li>Set <b>Schedule</b> and <b>Holidays</b> so times display correctly.</li>
              <li>Open <b>Booking Form</b> and remove any optional steps you do not want in the wizard.</li>
              <li>Publish the shortcode on a page and test one booking from start to finish.</li>
            </ol>
          </GuideCard>

          <GuideCard
            title="2) Publish the booking wizard"
            actions={[
              <ActionLink key="wizard" href={ADMIN_URLS.wizard} primary>Open Booking Form</ActionLink>,
            ]}
          >
            <div>Add this shortcode to any page where you want customers to open the booking wizard:</div>
            <Code>[pointlybooking_booking_form]</Code>

            <div style={{ marginTop: 12 }}>
              If you want different button text:
            </div>
            <Code>[pointlybooking_booking_form label="Book Now"]</Code>

            <div style={{ marginTop: 12 }}>
              After publishing the page, test the full customer flow yourself and confirm the wizard opens, moves through
              steps cleanly, and creates the booking.
            </div>
          </GuideCard>

          <GuideCard
            title="3) Make the wizard easier for customers"
            actions={[
              <ActionLink key="wizard" href={ADMIN_URLS.wizard}>Booking Form</ActionLink>,
              <ActionLink key="fields" href={ADMIN_URLS.formFields}>Form Fields</ActionLink>,
            ]}
          >
            <ul style={{ margin: 0, paddingLeft: 18 }}>
              <li>Use <b>Booking Form</b> to hide steps you do not need, such as Location, Category, Extras, or Payment.</li>
              <li>Edit step titles, helper text, and presentation so the wizard matches your business flow.</li>
              <li>Use <b>Form Fields</b> to add only the customer questions you actually need.</li>
              <li>Keep the booking flow short. Fewer fields and fewer steps usually convert better.</li>
            </ul>
          </GuideCard>

          <GuideCard
            title="4) Payments, reminders, and optional extras"
            actions={[
              <ActionLink key="payments" href={ADMIN_URLS.payments}>Payments</ActionLink>,
              <ActionLink key="automation" href={ADMIN_URLS.automation}>Automation</ActionLink>,
              <ActionLink key="coupons" href={ADMIN_URLS.coupons}>Coupons</ActionLink>,
            ]}
          >
            <ul style={{ margin: 0, paddingLeft: 18 }}>
              <li>Enable payment methods in <b>Settings -&gt; Payments</b> if you want online checkout.</li>
              <li>Use <b>Automation</b> for reminders, email flows, and follow-up behavior.</li>
              <li>Use <b>Coupons</b> if you run discounts or promotional campaigns.</li>
              <li>If you want a customer self-service area, create a page with this shortcode:</li>
            </ul>
            <Code>[pointlybooking_customer_portal]</Code>
          </GuideCard>

          <GuideCard title="5) Quick troubleshooting">
            <ul style={{ margin: 0, paddingLeft: 18 }}>
              <li>If the wizard does not open, confirm the page contains <code>[pointlybooking_booking_form]</code>.</li>
              <li>If no times appear, make sure the service has an active agent and the schedule is configured.</li>
              <li>If payments are missing, recheck your keys and enabled methods in <b>Payments</b>.</li>
              <li>If custom fields do not show, confirm they were added in <b>Form Fields</b> and saved correctly.</li>
              <li>If bookings look correct in the wizard but not in admin, review the <b>Calendar</b> and status flow after a test booking.</li>
            </ul>
          </GuideCard>
        </div>
      </main>
    </div>
  );
}
