/* global React */
// Generic legal/technical document page. Reads parsed prose docs from
// window.LEGAL_PARSED (landing/legal-content.js) and renders them, plus two
// hand-structured docs (Legal Notice, SLO Agreement). Routed from sections.jsx.

const LegalPage = (function () {
  const { Button } = window.CueDesignSystem_89ac8b;
  const { useEffect } = React;

  // Page titles by route key (the prose docs' own first line is skipped when
  // it duplicates this title).
  const TITLES = {
    "terms-of-use": "Terms of Use",
    "aaas-agreement": "Agent as a Service Agreement",
    "slo-agreement": "Service Level Objective",
    "responsibility": "Responsibility",
    "security": "Security",
    "privacy-policy": "Privacy Policy",
    "legal-notice": "Legal Notice",
  };

  // Hand-authored structured docs (the source files are short / tabular).
  const HAND = {
    "legal-notice": [
      { t: "h", x: "Company Information" },
      { t: "p", x: "Company name: QAECY" },
      { t: "p", x: "Legal form: AG" },
      { t: "p", x: "Registered office: Trockenloostrasse 21, 8105 Regensdorf, Switzerland" },
      { t: "h", x: "Authorized Representatives" },
      { t: "li", x: "Philipp Dohmen — CEO & Board" },
      { t: "li", x: "Felix Amberg — President of the Board" },
      { t: "li", x: "Yvette Körber — Board" },
      { t: "h", x: "Contact Details" },
      { t: "p", x: "Email: info@qaecy.com" },
      { t: "p", x: "Telephone: +41 44 505 77 93" },
      { t: "h", x: "Registry & Regulatory Information" },
      { t: "p", x: "Register court: Zürich" },
      { t: "p", x: "Commercial register number: CH-020.3.054.661-5" },
      { t: "p", x: "UID / VAT: CHE-215.106.128" },
      { t: "h", x: "Liability for Content" },
      { t: "p", x: "The content of our website has been compiled with the utmost care. However, we cannot guarantee the accuracy, completeness, or topicality of the information provided. As a service provider, we are responsible for our own content on these pages in accordance with general statutory laws." },
      { t: "h", x: "Liability for Links & Integrations" },
      { t: "p", x: "Our website may contain links, integrations, or references to third-party external websites and platforms (such as external data providers, analytics tools, or integrated software solutions). We have no influence over the current or future content of these external websites. The respective provider or operator of those networks is always solely responsible for the content of the linked pages." },
      { t: "h", x: "Copyright Notice" },
      { t: "p", x: "The content, layout, and works published on this website are protected by copyright law. Any duplication, processing, distribution, or commercial utilization outside the scope of copyright law requires the prior written consent of the respective author or creator." },
    ],
    "slo-agreement": [
      { t: "p", x: "During the Term, QAECY will use commercially reasonable efforts to make the QAECY API AaaS Services available on a 24 × 7 × 365 basis, with the objective of achieving the Monthly Uptime Percentages set out below (the \"Service Level Objective\" or \"SLO\")." },
      { t: "h", x: "Covered Services" },
      { t: "li", x: "dashboard.qaecy.ai" },
      { t: "li", x: "api.qaecy.ai" },
      { t: "h", x: "Service Level Credits" },
      { t: "p", x: "If QAECY fails to meet the SLO in any calendar month during the Term, and if Customer meets its obligations under the Agreement, Customer will be entitled to the following credits — calculated as a percentage of the monthly invoice for the Covered Service that did not meet the SLO, applied to Customer's next monthly invoice:" },
      { t: "li", x: "Monthly uptime ≥ 95% — no credit" },
      { t: "li", x: "94% to < 95% — 10% of the monthly invoice credited" },
      { t: "li", x: "93% to < 94% — 20% of the monthly invoice credited" },
      { t: "li", x: "≤ 93% — 30% of the monthly invoice credited" },
      { t: "p", x: "The Service Level Credits are Customer's sole and exclusive remedy, and QAECY's sole and entire obligation and liability, in respect of any failure of QAECY to meet the SLO." },
      { t: "h", x: "Claiming Credits" },
      { t: "p", x: "To receive any Service Level Credit, Customer must notify QAECY at support@qaecy.ai within 30 days from the time Customer becomes entitled to the credit, providing the date and time of the SLO failure and any additional information requested by QAECY. If Customer does not comply with these requirements, Customer forfeits its right to receive a Service Level Credit. Based on the information provided and QAECY's internal system logs, QAECY will determine, in its sole discretion, whether Customer will receive a credit." },
      { t: "p", x: "Service Level Credits apply to outstanding or future invoices only and are forfeited upon termination or expiration of the Agreement. QAECY is not required to issue refunds or make payments against such credits under any circumstances. QAECY may unilaterally amend this SLO, in whole or in part, by giving Customer prior notice or posting notice on the Website." },
      { t: "h", x: "SLO Exclusions" },
      { t: "p", x: "The SLO does not apply to any features within the Services that are marked as Alpha or Beta, or any features excluded from the Services as stated in QAECY's documentation." },
      { t: "h", x: "Definitions" },
      { t: "p", x: "Capitalized terms used but not defined herein have the meanings set out in the QAECY Agent as a Service Agreement or Terms of Use (collectively, the \"Agreement\")." },
      { t: "p", x: "\"Covered Service\" means the services that comprise the QAECY API AaaS Services available at the QAECY Websites." },
      { t: "p", x: "\"Monthly Uptime Percentage\" means the total percentage of availability of a Service within a given month, excluding unavailability due to: (i) acts or omissions of Customer or its Permitted Users; (ii) Customer's failure to adhere to QAECY's recommendations, including minimum system requirements; (iii) acts or omissions of QAECY when acting under Customer's direction; (iv) spikes in demand not previously agreed in writing; (v) scheduled or emergency maintenance; (vi) downtime of third-party service providers; (vii) Force Majeure; or (viii) suspension permitted under the Agreement." },
    ],
  };

  function norm(s) { return s.toLowerCase().replace(/[^a-z0-9]/g, ""); }

  function renderPara(x, key) {
    // Bold a short lead label of the form "Label. Rest of sentence."
    const m = x.match(/^([A-Z][A-Za-z0-9 ,'&()\/-]{1,42})\.\s+([A-Z].*)$/);
    if (m && !/\./.test(m[1])) {
      return <p className="p doc__p" key={key}><strong>{m[1]}.</strong> {m[2]}</p>;
    }
    return <p className="p doc__p" key={key}>{x}</p>;
  }

  function Blocks({ blocks, title }) {
    const out = [];
    let list = null;
    blocks.forEach((b, i) => {
      // skip a leading heading that just repeats the page title
      if (i === 0 && b.t === "h" && norm(b.x) === norm(title)) return;
      if (b.t === "li") { (list = list || []).push(<li key={i}>{b.x}</li>); return; }
      if (list) { out.push(<ul className="doc__ul" key={"ul" + i}>{list}</ul>); list = null; }
      if (b.t === "h") out.push(<h2 className="h3 doc__legal-h" key={i}>{b.x}</h2>);
      else out.push(renderPara(b.x, i));
    });
    if (list) out.push(<ul className="doc__ul" key="ul-end">{list}</ul>);
    return <React.Fragment>{out}</React.Fragment>;
  }

  function LegalPage({ route }) {
    useEffect(() => { window.scrollTo(0, 0); }, [route]);
    const title = TITLES[route] || "Legal";
    const blocks = (window.LEGAL_PARSED && window.LEGAL_PARSED[route]) || HAND[route] || [];
    return (
      <main className="doc section section--canvas" data-screen-label={title}>
        <div className="container">
          <div className="doc__wrap">
            <a className="doc__back" href="#top">
              <span className="cue-icon">arrow_back</span> Back to home
            </a>
            <h1 className="h1 doc__title">{title}</h1>
            <div className="doc__legal">
              <Blocks blocks={blocks} title={title} />
            </div>
            <div className="doc__divider" />
            <div className="doc__footer-row">
              <span className="doc__updated">QAECY AG · Regensdorf, Switzerland</span>
              <a href="#top"><Button variant="secondary" size="m" leadingIcon="arrow_back">Back to home</Button></a>
            </div>
          </div>
        </div>
      </main>
    );
  }

  return LegalPage;
})();

window.LegalPage = LegalPage;
