"use client";

import Link from "next/link";
import { FileText } from "lucide-react";
import { MarketingNav } from "@/components/marketing/nav";
import { MarketingFooter } from "@/components/marketing/footer";

function Section({
  id,
  title,
  children,
}: {
  id: string;
  title: string;
  children: React.ReactNode;
}) {
  return (
    <section id={id} className="scroll-mt-24">
      <h2 className="text-xl font-bold tracking-tight mb-4">{title}</h2>
      <div className="space-y-4 text-[15px] leading-relaxed text-muted-foreground">
        {children}
      </div>
    </section>
  );
}

const EFFECTIVE_DATE = "April 16, 2026";

export default function TermsOfServicePage() {
  return (
    <div className="min-h-screen bg-background">
      <MarketingNav />

      {/* Hero */}
      <section className="relative pt-32 pb-12 sm:pt-40 sm:pb-16 overflow-hidden">
        <div className="absolute inset-0 gradient-mesh pointer-events-none" />
        <div className="relative mx-auto max-w-3xl px-6 text-center">
          <div className="inline-flex items-center gap-2 rounded-full border border-border/60 bg-card/60 px-4 py-1.5 text-xs font-medium text-muted-foreground mb-8 glass">
            <FileText className="h-3.5 w-3.5" />
            Legal
          </div>
          <h1 className="text-3xl sm:text-4xl font-bold tracking-tight leading-[1.1] mb-4">
            Terms of Service
          </h1>
          <p className="text-muted-foreground">
            Effective {EFFECTIVE_DATE}
          </p>
        </div>
      </section>

      {/* Body */}
      <article className="mx-auto max-w-3xl px-6 pb-24 space-y-12">
        {/* Intro */}
        <div className="text-[15px] leading-relaxed text-muted-foreground space-y-4">
          <p>
            These Terms of Service (&ldquo;Terms&rdquo;) govern your access to and use of the
            Echo platform (&ldquo;Service&rdquo;), operated by Echo (&ldquo;we,&rdquo;
            &ldquo;us,&rdquo; or &ldquo;our&rdquo;). By creating an account or using the
            Service, you agree to be bound by these Terms.
          </p>
          <p>
            If you are using the Service on behalf of a church, ministry, nonprofit, or other
            organization, you represent that you have the authority to bind that organization
            to these Terms, and &ldquo;you&rdquo; refers to both you individually and the
            organization.
          </p>
        </div>

        {/* TOC */}
        <nav className="rounded-xl border border-border/50 bg-card/40 p-6 glass-card">
          <h2 className="text-sm font-semibold mb-3">Contents</h2>
          <ol className="grid sm:grid-cols-2 gap-x-8 gap-y-1.5 text-sm text-muted-foreground list-decimal list-inside">
            {[
              ["#service-description", "Description of Service"],
              ["#eligibility", "Eligibility"],
              ["#accounts", "Accounts & Access"],
              ["#acceptable-use", "Acceptable Use"],
              ["#your-data", "Your Data & Responsibility"],
              ["#data-processing", "Data Processing"],
              ["#subscriptions", "Subscriptions & Billing"],
              ["#free-tier", "Free Tier"],
              ["#intellectual-property", "Intellectual Property"],
              ["#integrations", "Third-Party Integrations"],
              ["#availability", "Service Availability"],
              ["#limitation", "Limitation of Liability"],
              ["#indemnification", "Indemnification"],
              ["#termination", "Termination"],
              ["#changes", "Changes to These Terms"],
              ["#governing-law", "Governing Law"],
              ["#contact", "Contact Us"],
            ].map(([href, label]) => (
              <li key={href}>
                <a href={href} className="hover:text-foreground transition-colors">
                  {label}
                </a>
              </li>
            ))}
          </ol>
        </nav>

        {/* 1 */}
        <Section id="service-description" title="1. Description of Service">
          <p>
            Echo is a cloud-based platform that uses artificial intelligence to extract
            structured data from scanned response cards, guest cards, prayer request forms,
            and similar paper documents. The Service includes:
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>AI-powered optical character recognition (OCR) for handwritten and printed text</li>
            <li>Document ingestion via web upload, email, and FTP</li>
            <li>A team dashboard for reviewing, assigning, and managing extracted data</li>
            <li>Integrations with third-party services (Planning Center, Google Sheets, Monday.com, Airtable, webhooks)</li>
            <li>Collection day and event management</li>
            <li>Multi-site organization support</li>
            <li>Data export via CSV and API</li>
          </ul>
        </Section>

        {/* 2 */}
        <Section id="eligibility" title="2. Eligibility">
          <p>
            You must be at least 18 years old (or the age of majority in your jurisdiction)
            to create an account. By using the Service, you represent that you meet this
            requirement. The Service is designed for use by churches, ministries, nonprofits,
            and similar organizations.
          </p>
        </Section>

        {/* 3 */}
        <Section id="accounts" title="3. Accounts & Access">
          <p>
            You are responsible for maintaining the confidentiality of your account
            credentials and for all activity that occurs under your account. You agree to:
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>Provide accurate and complete registration information</li>
            <li>Keep your password secure and not share it with others</li>
            <li>Notify us immediately if you suspect unauthorized access to your account</li>
            <li>Not create accounts for automated use without our prior written consent</li>
          </ul>
          <p>
            Organization owners and administrators are responsible for managing team member
            access, assigning appropriate roles, and removing members who should no longer
            have access.
          </p>
        </Section>

        {/* 4 */}
        <Section id="acceptable-use" title="4. Acceptable Use">
          <p>You agree not to:</p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>Use the Service for any unlawful purpose or in violation of any applicable law</li>
            <li>Upload content that you do not have the right to process (e.g., cards collected without consent)</li>
            <li>Attempt to gain unauthorized access to the Service, other users&rsquo; accounts, or our infrastructure</li>
            <li>Interfere with or disrupt the integrity or performance of the Service</li>
            <li>Reverse engineer, decompile, or disassemble any part of the Service</li>
            <li>Use the Service to build a competing product</li>
            <li>Resell or redistribute access to the Service without our written consent</li>
            <li>Upload malicious files, malware, or content designed to exploit the system</li>
          </ul>
          <p>
            We reserve the right to suspend or terminate accounts that violate these
            restrictions.
          </p>
        </Section>

        {/* 5 */}
        <Section id="your-data" title="5. Your Data & Responsibility">
          <p>
            You retain all rights to the data you upload to the Service (&ldquo;Your
            Data&rdquo;). This includes scanned card images, extracted text and structured
            fields, and any other content you provide.
          </p>
          <p>
            <strong>You are the data controller.</strong> As the organization using Echo, you
            are responsible for:
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>
              Ensuring you have appropriate consent or legal basis to collect and process the
              personal information on scanned cards
            </li>
            <li>
              Complying with applicable data protection laws (including GDPR, CCPA, state
              privacy laws, and any church- or denomination-specific data policies)
            </li>
            <li>
              Responding to data subject requests (access, correction, deletion) from
              individuals whose information appears on scanned cards
            </li>
            <li>
              Configuring appropriate data retention settings and deleting data when it is
              no longer needed
            </li>
          </ul>
          <p>
            Echo acts as a <strong>data processor</strong> on your behalf. We process Your
            Data only as necessary to provide the Service and in accordance with our{" "}
            <Link href="/privacy" className="underline hover:text-foreground">
              Privacy Policy
            </Link>
            .
          </p>
        </Section>

        {/* 6 */}
        <Section id="data-processing" title="6. Data Processing">
          <p>
            By using the Service, you authorize us to process Your Data as described in our{" "}
            <Link href="/privacy" className="underline hover:text-foreground">
              Privacy Policy
            </Link>
            , including:
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>
              Transmitting card images to AI model providers for OCR extraction (by default,
              OpenAI via the Vercel AI Gateway)
            </li>
            <li>
              Storing Your Data on our infrastructure providers (Supabase for database and
              object storage, Vercel for hosting)
            </li>
            <li>
              Sending Your Data to third-party integrations that you have explicitly
              configured (Planning Center, Google Sheets, etc.)
            </li>
            <li>
              Using Brevo to deliver transactional emails to your team members
            </li>
          </ul>
          <p>
            We will not process Your Data for any purpose other than providing, maintaining,
            and improving the Service. We will not sell, rent, or share Your Data with third
            parties for their own purposes.
          </p>
        </Section>

        {/* 7 */}
        <Section id="subscriptions" title="7. Subscriptions & Billing">
          <p>
            Paid plans are billed on a monthly or annual basis as selected at the time of
            purchase. By subscribing to a paid plan, you agree to pay the applicable fees.
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>
              <strong>Billing cycle</strong> &mdash; charges are billed in advance at the
              start of each billing period
            </li>
            <li>
              <strong>Card limits</strong> &mdash; each plan includes a monthly card
              processing allowance. Cards exceeding the limit are billed at the overage rate
              specified in your plan ($0.08/card unless otherwise agreed)
            </li>
            <li>
              <strong>Upgrades and downgrades</strong> &mdash; you may change your plan at
              any time. Upgrades take effect immediately with prorated charges; downgrades
              take effect at the next billing cycle
            </li>
            <li>
              <strong>Cancellation</strong> &mdash; you may cancel your subscription at any
              time. Cancellation takes effect at the end of the current billing period. No
              refunds are provided for partial periods
            </li>
            <li>
              <strong>Free trials</strong> &mdash; paid plans include a 14-day free trial.
              If you do not cancel before the trial ends, you will be charged for the first
              billing period
            </li>
          </ul>
          <p>
            We reserve the right to change pricing with 30 days&rsquo; notice. Price changes
            will not affect your current billing period.
          </p>
        </Section>

        {/* 8 */}
        <Section id="free-tier" title="8. Free Tier">
          <p>
            Echo offers a free tier with limited features and a monthly card processing
            cap. The free tier is provided &ldquo;as is&rdquo; and may be modified or
            discontinued at any time with reasonable notice.
          </p>
          <p>
            Free tier accounts that remain inactive (no cards processed, no logins) for
            12 consecutive months may be subject to deletion after a 30-day notice email.
          </p>
        </Section>

        {/* 9 */}
        <Section id="intellectual-property" title="9. Intellectual Property">
          <p>
            The Service, including its design, code, features, documentation, and branding,
            is owned by Echo and protected by intellectual property laws. These Terms do not
            grant you any rights to our trademarks, logos, or brand assets.
          </p>
          <p>
            You grant us a limited, non-exclusive license to use, store, and process Your
            Data solely for the purpose of providing the Service. This license terminates
            when you delete Your Data or close your account.
          </p>
        </Section>

        {/* 10 */}
        <Section id="integrations" title="10. Third-Party Integrations">
          <p>
            The Service allows you to connect to third-party services. Your use of those
            services is governed by their own terms and privacy policies. We are not
            responsible for the availability, accuracy, or practices of third-party services.
          </p>
          <p>
            You are responsible for providing valid credentials and ensuring that your use of
            integrations complies with the third party&rsquo;s terms. We may disable an
            integration if it causes service instability or violates these Terms.
          </p>
        </Section>

        {/* 11 */}
        <Section id="availability" title="11. Service Availability">
          <p>
            We strive to maintain high availability but do not guarantee uninterrupted
            access. The Service may be temporarily unavailable due to maintenance, updates,
            or circumstances beyond our control.
          </p>
          <p>
            Enterprise plans include a 99.9% uptime SLA. Details of the SLA, including
            remedies for downtime, are provided in the Enterprise agreement.
          </p>
          <p>
            We will make reasonable efforts to notify you of planned maintenance in advance.
          </p>
        </Section>

        {/* 12 */}
        <Section id="limitation" title="12. Limitation of Liability">
          <p>
            TO THE MAXIMUM EXTENT PERMITTED BY LAW, ECHO SHALL NOT BE LIABLE FOR ANY
            INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES, OR ANY LOSS
            OF PROFITS, DATA, OR GOODWILL, ARISING OUT OF OR RELATED TO YOUR USE OF THE
            SERVICE.
          </p>
          <p>
            OUR TOTAL AGGREGATE LIABILITY FOR ALL CLAIMS RELATED TO THE SERVICE SHALL NOT
            EXCEED THE AMOUNT YOU PAID US IN THE 12 MONTHS PRECEDING THE CLAIM, OR $100,
            WHICHEVER IS GREATER.
          </p>
          <p>
            THE SERVICE IS PROVIDED &ldquo;AS IS&rdquo; AND &ldquo;AS AVAILABLE&rdquo;
            WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
            WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
            NON-INFRINGEMENT.
          </p>
          <p>
            AI-powered OCR is not 100% accurate. You are responsible for reviewing extracted
            data before relying on it or syncing it to external systems.
          </p>
        </Section>

        {/* 13 */}
        <Section id="indemnification" title="13. Indemnification">
          <p>
            You agree to indemnify, defend, and hold harmless Echo and its officers,
            employees, and agents from any claims, liabilities, damages, losses, or
            expenses (including reasonable attorneys&rsquo; fees) arising out of:
          </p>
          <ul className="list-disc list-inside space-y-1 ml-2">
            <li>Your use of the Service</li>
            <li>Your violation of these Terms</li>
            <li>Your violation of any applicable law or regulation</li>
            <li>Your failure to obtain appropriate consent for the personal data you process through the Service</li>
          </ul>
        </Section>

        {/* 14 */}
        <Section id="termination" title="14. Termination">
          <p>
            Either party may terminate these Terms at any time. You may stop using the
            Service and delete your account by contacting{" "}
            <a
              href="mailto:support@stillwell.cloud"
              className="underline hover:text-foreground"
            >
              support@stillwell.cloud
            </a>
            .
          </p>
          <p>
            We may suspend or terminate your access if you violate these Terms, fail to pay
            applicable fees, or if we are required to do so by law. We will provide
            reasonable notice before termination, except in cases of severe violation.
          </p>
          <p>
            Upon termination, your right to use the Service ceases. You may export Your
            Data before termination. We will retain Your Data for 30 days after
            termination to allow for export, after which it will be permanently deleted.
          </p>
        </Section>

        {/* 15 */}
        <Section id="changes" title="15. Changes to These Terms">
          <p>
            We may update these Terms from time to time. If we make material changes, we
            will notify you by posting a prominent notice on the Service or sending you an
            email at least 30 days before the changes take effect. Your continued use of
            the Service after changes take effect constitutes acceptance.
          </p>
        </Section>

        {/* 16 */}
        <Section id="governing-law" title="16. Governing Law">
          <p>
            These Terms are governed by and construed in accordance with the laws of the
            State of Florida, without regard to its conflict of law provisions. Any disputes
            arising under these Terms shall be resolved in the state or federal courts
            located in Escambia County, Florida.
          </p>
        </Section>

        {/* 17 */}
        <Section id="contact" title="17. Contact Us">
          <p>
            If you have questions about these Terms, please contact us:
          </p>
          <ul className="list-none space-y-1 ml-2">
            <li>
              Legal:{" "}
              <a
                href="mailto:support@stillwell.cloud?subject=Legal%20inquiry"
                className="underline hover:text-foreground"
              >
                support@stillwell.cloud
              </a>
            </li>
            <li>
              General support:{" "}
              <a
                href="mailto:support@stillwell.cloud"
                className="underline hover:text-foreground"
              >
                support@stillwell.cloud
              </a>
            </li>
          </ul>
        </Section>
      </article>

      <MarketingFooter />
    </div>
  );
}
