import { useAnalyticsConsent } from "../core/privacy/analytics-consent-context";
import { privacyConfig } from "../core/privacy/privacy-config";

function Section({
  title,
  children,
}: {
  title: string;
  children: React.ReactNode;
}) {
  return (
    <section className="rounded-[18px] border border-[var(--p-border)] bg-[var(--p-surface)] p-5 md:p-7">
      <h2 className="m-0 font-sans text-xl font-semibold tracking-tight text-[var(--p-text)]">
        {title}
      </h2>
      <div className="mt-4 space-y-3 text-sm leading-relaxed text-[var(--p-muted)]">
        {children}
      </div>
    </section>
  );
}

export function PrivacyPage() {
  const { choice, openSettings } = useAnalyticsConsent();

  return (
    <div className="mx-auto flex max-w-[920px] flex-col gap-6 pb-16 animate-plimi-slide">
      <header className="flex flex-col gap-4">
        <span className="font-mono text-[11px] uppercase tracking-[0.12em] text-[var(--p-muted)]">
          privacy notice & legal information
        </span>
        <h1 className="m-0 font-sans text-4xl font-bold leading-[1.03] tracking-tight text-[var(--p-text)] md:text-[52px]">
          Your tools are local. Analytics are optional.
        </h1>
        <p className="m-0 max-w-[760px] text-base leading-relaxed text-[var(--p-muted)]">
          Effective {privacyConfig.policyEffectiveDate}. This notice explains
          who operates Plimi, what data the website processes, and the choices
          available to you.
        </p>
      </header>

      <Section title="Controller and contact">
        <p className="m-0">
          The data controller is{" "}
          <strong className="text-[var(--p-text)]">
            {privacyConfig.controllerName}
          </strong>
          , based in {privacyConfig.controllerCountry}.
        </p>
        <p className="m-0">
          Privacy requests:{" "}
          <a
            href={`mailto:${privacyConfig.privacyEmail}`}
            className="text-[var(--p-text)] underline underline-offset-4"
          >
            {privacyConfig.privacyEmail}
          </a>
          . Website: {privacyConfig.siteUrl}.
        </p>
      </Section>

      <Section title="Local tool processing">
        <p className="m-0">
          Text, files, images, PDFs, generated passwords, and tool outputs are
          processed by code running in your browser. Plimi does not upload this
          tool content to an application server or include it in analytics.
        </p>
        <p className="m-0">
          Files you download remain under your control. Browser features such
          as the clipboard or local file picker are used only when you invoke
          them.
        </p>
      </Section>

      <Section title="Optional analytics">
        <p className="m-0">
          Analytics are disabled until you consent. If accepted, the site loads{" "}
          {privacyConfig.analyticsProvider} from {privacyConfig.analyticsHost}.
          It may process the visited route, referrer, timestamp, browser,
          operating system, device type, screen size, language, and an
          approximate country derived from the IP address. Umami states that it
          does not store the IP address, but uses request and device information
          to generate a session identifier.
        </p>
        <p className="m-0">
          Purpose: aggregate audience measurement and improvement of Plimi.
          Plimi does not use analytics for advertising, profiling, sharing with
          data brokers, or cross-site tracking. Analytics data is configured
          for a retention period of{" "}
          {privacyConfig.analyticsRetentionMonths} months and is hosted in{" "}
          {privacyConfig.analyticsHostCountry}.
        </p>
        <p className="m-0">
          Plimi does not send tool inputs, uploaded file contents, outputs,
          names, email addresses, account identifiers, custom analytics events,
          or `umami.identify()` data. The tracker excludes URL search
          parameters and hash fragments and honors browser Do Not Track.
        </p>
        <p className="m-0">
          The legal basis is your consent under Article 6(1)(a) GDPR. Umami is
          not loaded and no analytics request is sent unless you select
          &quot;Accept analytics&quot;. You may withdraw consent at any time,
          without affecting processing that occurred before withdrawal.
        </p>
        <div className="flex flex-wrap items-center gap-3 pt-2">
          <button
            onClick={openSettings}
            className="rounded-[10px] bg-[var(--p-accent)] px-4 py-2.5 font-sans text-sm font-semibold text-[var(--p-accent-ink)]"
          >
            Change analytics choice
          </button>
          <span className="font-mono text-[11px] uppercase text-[var(--p-muted)]">
            Current choice:{" "}
            {choice === "granted"
              ? "accepted"
              : choice === "denied"
                ? "rejected"
                : "not chosen"}
          </span>
        </div>
        <p className="m-0">
          Your choice is stored locally as{" "}
          <code className="text-[var(--p-text)]">
            localStorage[&quot;plimi.analytics-consent.v1&quot;]
          </code>
          . You can change or withdraw it at any time using these controls.
        </p>
      </Section>

      <Section title="Local storage and essential preferences">
        <p className="m-0">
          Plimi stores your theme preference and analytics consent preference in
          browser local storage. These values are used only to remember your
          settings. Plimi does not use analytics cookies, advertising cookies,
          or marketing pixels.
        </p>
        <p className="m-0">
          If you use automatic background removal, Plimi stores the selected AI
          model and its ONNX runtime files in your browser&apos;s Cache Storage.
          This avoids downloading the same large files for every run. The cache
          contains application assets only, not your uploaded images, masks, or
          generated results. You can delete it at any time with the
          &quot;Clear models&quot; button in the Background Remover.
        </p>
      </Section>

      <Section title="Recipients and transfers">
        <p className="m-0">
          When analytics are enabled, analytics metadata is received by the
          operator of {privacyConfig.analyticsHost} and any infrastructure
          providers used to host it. Plimi does not load other non-essential
          trackers, third-party embeds, advertising pixels, or cross-site
          analytics services.
        </p>
      </Section>

      <Section title="Your rights">
        <p className="m-0">
          Depending on applicable law, you may request access, correction,
          deletion, restriction, or portability of your personal data. You may
          opt out of analytics at any time through the controls on this page or
          in the footer.
        </p>
        <p className="m-0">
          Contact {privacyConfig.privacyEmail} to exercise a right. You may also
          lodge a complaint with the data-protection authority in your country
          of residence, work, or the place of an alleged infringement.
        </p>
      </Section>

      <Section title="Security and changes">
        <p className="m-0">
          Plimi uses HTTPS and access controls appropriate to the services it
          operates. No internet service can guarantee absolute security. This
          notice may be updated when processing or providers change; the
          effective date above identifies the current version.
        </p>
      </Section>
    </div>
  );
}
