import { writeFileSync, mkdirSync } from "fs";

// ─── Update settings/page.tsx to include 2FA link ────────────────────────────
writeFileSync("app/settings/page.tsx", `import Link from "next/link";
import { AppShell } from "../../components/app-shell";

const settingsItems = [
  {
    title: "Profile",
    description: "Update company details, contact info, and onboarding fields.",
    href: "/settings/profile",
  },
  {
    title: "Two-Factor Auth",
    description: "Add a TOTP authenticator app for extra security.",
    href: "/settings/2fa",
  },
  {
    title: "Subscription",
    description: "View plan details, upgrade options, and billing cadence.",
    href: "/settings/subscription",
  },
];

export default function SettingsPage() {
  return (
    <AppShell title="Settings" subtitle="Account preferences and plan configuration.">
      <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
        {settingsItems.map((item) => (
          <Link
            key={item.title}
            href={item.href}
            className="glass-panel p-6 rounded-2xl shadow-sm transition-all hover:-translate-y-1 hover:border-primary/50 group"
          >
            <p className="text-lg font-bold text-foreground group-hover:text-primary transition-colors">{item.title}</p>
            <p className="mt-2 text-sm text-muted-foreground">{item.description}</p>
            <span className="mt-4 inline-flex rounded-full border border-border bg-secondary/50 px-3 py-1 text-xs font-medium text-muted-foreground">
              Open
            </span>
          </Link>
        ))}
      </div>
    </AppShell>
  );
}
`);

// ─── settings/2fa/page.tsx ───────────────────────────────────────────────────
mkdirSync("app/settings/2fa", { recursive: true });
writeFileSync("app/settings/2fa/page.tsx", `"use client";

import { useEffect, useState } from "react";
import { AppShell } from "../../../components/app-shell";
import { apiFetch } from "../../../lib/api";

type ApiResponse<T> = {
  data: T;
  meta: { timestamp: string; version: "v1" };
  error: null | { message: string; code?: string };
};

type TwoFaGenerateData = { qrCode: string; otpAuthUrl: string };
type UserData = { user: { twoFactorEnabled: boolean } };

export default function TwoFAPage() {
  const [enabled, setEnabled] = useState<boolean | null>(null);
  const [qrCode, setQrCode] = useState<string>("");
  const [otpAuthUrl, setOtpAuthUrl] = useState<string>("");
  const [token, setToken] = useState("");
  const [status, setStatus] = useState("");
  const [isError, setIsError] = useState(false);
  const [step, setStep] = useState<"idle" | "scan" | "done">("idle");

  useEffect(() => {
    apiFetch<UserData["user"]>("/api/auth/me")
      .then((res) => {
        if (!res.error && res.data) {
          // me returns { user: {...} }
          const data = res.data as unknown as UserData;
          setEnabled(data.user?.twoFactorEnabled ?? false);
        }
      })
      .catch(() => {});
  }, []);

  const handleGenerate = async () => {
    setStatus("Generating QR code...");
    setIsError(false);
    const res = await apiFetch<TwoFaGenerateData>("/api/2fa/generate", { method: "POST" });
    if (res.error) {
      setStatus(res.error.message ?? "Failed to generate 2FA secret.");
      setIsError(true);
      return;
    }
    setQrCode(res.data.qrCode);
    setOtpAuthUrl(res.data.otpAuthUrl);
    setStep("scan");
    setStatus("Scan the QR code with your authenticator app, then enter the code below.");
  };

  const handleEnable = async (event: React.FormEvent) => {
    event.preventDefault();
    if (!token || token.length !== 6) {
      setStatus("Please enter the 6-digit code.");
      setIsError(true);
      return;
    }
    setStatus("Verifying...");
    setIsError(false);
    const res = await apiFetch<{ message: string }>("/api/2fa/enable", {
      method: "POST",
      body: JSON.stringify({ token }),
    });
    if (res.error) {
      setStatus(res.error.message ?? "Verification failed. Try again.");
      setIsError(true);
      return;
    }
    setEnabled(true);
    setStep("done");
    setStatus("Two-factor authentication is now active.");
  };

  const handleDisable = async (event: React.FormEvent) => {
    event.preventDefault();
    if (!token || token.length !== 6) {
      setStatus("Please enter the 6-digit code to confirm.");
      setIsError(true);
      return;
    }
    setStatus("Disabling 2FA...");
    setIsError(false);
    const res = await apiFetch<{ message: string }>("/api/2fa/disable", {
      method: "DELETE",
      body: JSON.stringify({ token }),
    });
    if (res.error) {
      setStatus(res.error.message ?? "Failed. Check your authenticator code.");
      setIsError(true);
      return;
    }
    setEnabled(false);
    setToken("");
    setStep("idle");
    setStatus("Two-factor authentication has been disabled.");
  };

  return (
    <AppShell title="Two-Factor Auth" subtitle="Secure your account with a TOTP authenticator.">
      <div className="max-w-lg">
        <div className="glass-panel rounded-2xl p-8">
          {enabled === null ? (
            <div className="flex items-center justify-center py-8">
              <div className="h-8 w-8 rounded-full border-2 border-primary border-t-transparent animate-spin" />
            </div>
          ) : enabled ? (
            <>
              <div className="flex items-center gap-3 mb-6">
                <div className="h-10 w-10 rounded-full bg-primary/10 flex items-center justify-center">
                  <svg className="h-5 w-5 text-primary" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                  </svg>
                </div>
                <div>
                  <p className="text-sm font-bold text-foreground">2FA is Active</p>
                  <p className="text-xs text-muted-foreground">Your account is protected with TOTP.</p>
                </div>
              </div>

              <p className="text-sm text-muted-foreground mb-6">
                To disable two-factor authentication, enter the current code from your authenticator app.
              </p>

              <form onSubmit={handleDisable} className="space-y-4">
                <div>
                  <label className="block text-sm font-medium text-foreground mb-1">Authenticator Code</label>
                  <input
                    type="text" inputMode="numeric" maxLength={6} placeholder="000000"
                    value={token} onChange={(e) => setToken(e.target.value.replace(/\\D/g, ""))}
                    className="block w-full rounded-lg border border-border bg-background/50 px-3 py-2 text-sm text-center tracking-widest focus:border-primary focus:outline-none focus:ring-primary transition-all"
                    required
                  />
                </div>
                <button
                  type="submit"
                  className="w-full rounded-lg border border-red-500/30 bg-red-500/10 py-2.5 px-4 text-sm font-bold text-red-500 hover:bg-red-500/20 transition-all"
                >
                  Disable 2FA
                </button>
              </form>
            </>
          ) : step === "idle" ? (
            <>
              <div className="flex items-center gap-3 mb-6">
                <div className="h-10 w-10 rounded-full bg-secondary flex items-center justify-center">
                  <svg className="h-5 w-5 text-muted-foreground" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                  </svg>
                </div>
                <div>
                  <p className="text-sm font-bold text-foreground">2FA Not Enabled</p>
                  <p className="text-xs text-muted-foreground">Add an extra layer of protection.</p>
                </div>
              </div>
              <p className="text-sm text-muted-foreground mb-6">
                Use any TOTP authenticator app (Google Authenticator, Authy, 1Password) to generate login codes.
              </p>
              <button
                onClick={handleGenerate}
                className="w-full rounded-lg bg-primary py-2.5 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all"
              >
                Enable Two-Factor Auth
              </button>
            </>
          ) : step === "scan" ? (
            <>
              <p className="text-sm font-bold text-foreground mb-2">Scan this QR code</p>
              <p className="text-xs text-muted-foreground mb-4">
                Open your authenticator app and scan the code below, or enter the key manually.
              </p>
              {qrCode && (
                <div className="flex justify-center mb-4 bg-white p-3 rounded-xl inline-block">
                  {/* eslint-disable-next-line @next/next/no-img-element */}
                  <img src={qrCode} alt="2FA QR Code" className="h-40 w-40" />
                </div>
              )}
              {otpAuthUrl && (
                <p className="text-[10px] text-muted-foreground break-all mb-4 font-mono bg-secondary/30 p-2 rounded">{otpAuthUrl}</p>
              )}
              <form onSubmit={handleEnable} className="space-y-4">
                <div>
                  <label className="block text-sm font-medium text-foreground mb-1">Enter code to confirm</label>
                  <input
                    type="text" inputMode="numeric" maxLength={6} placeholder="6-digit code"
                    value={token} onChange={(e) => setToken(e.target.value.replace(/\\D/g, ""))}
                    className="block w-full rounded-lg border border-border bg-background/50 px-3 py-2 text-sm text-center tracking-widest focus:border-primary focus:outline-none focus:ring-primary transition-all"
                    required autoFocus
                  />
                </div>
                <button
                  type="submit"
                  className="w-full rounded-lg bg-primary py-2.5 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all"
                >
                  Verify and Enable
                </button>
              </form>
            </>
          ) : (
            <div className="text-center py-4">
              <div className="mx-auto h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center mb-4">
                <svg className="h-6 w-6 text-primary" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                </svg>
              </div>
              <p className="text-sm font-bold text-foreground">2FA Enabled Successfully</p>
              <p className="text-xs text-muted-foreground mt-1">Your account now requires a code on each login.</p>
            </div>
          )}

          {status && (
            <div className={\`mt-4 rounded-lg p-3 text-sm text-center \${isError ? "bg-red-500/10 border border-red-500/20 text-red-400" : "bg-accent/10 border border-accent/20"}\`}>
              {status}
            </div>
          )}
        </div>
      </div>
    </AppShell>
  );
}
`);

// ─── settings/subscription/page.tsx ─────────────────────────────────────────
mkdirSync("app/settings/subscription", { recursive: true });
writeFileSync("app/settings/subscription/page.tsx", `"use client";

import { useEffect, useState } from "react";
import { AppShell } from "../../../components/app-shell";
import { apiFetch } from "../../../lib/api";

type ApiResponse<T> = {
  data: T;
  meta: { timestamp: string; version: "v1" };
  error: null | { message: string; code?: string };
};

type SubscriptionData = {
  plan?: string;
  status?: string;
  billingCycleAnchor?: number;
  cancelAtPeriodEnd?: boolean;
};

const PLAN_LABELS: Record<string, string> = {
  free: "Free",
  pro: "Pro",
  elite: "Elite",
};

const PLAN_DESCRIPTIONS: Record<string, string> = {
  free: "Up to 2 accounts, basic CSV export, 30-day history.",
  pro: "Unlimited accounts, Google Sheets, 24-month history, priority support.",
  elite: "Everything in Pro + tax return module, AI rule suggestions, dedicated support.",
};

export default function SubscriptionPage() {
  const [sub, setSub] = useState<SubscriptionData | null>(null);
  const [loading, setLoading] = useState(true);
  const [actionStatus, setActionStatus] = useState("");
  const [actionLoading, setActionLoading] = useState(false);

  useEffect(() => {
    apiFetch<SubscriptionData>("/api/stripe/subscription")
      .then((res) => {
        if (!res.error) setSub(res.data);
      })
      .catch(() => {})
      .finally(() => setLoading(false));
  }, []);

  const handleUpgrade = async (plan: string) => {
    setActionLoading(true);
    setActionStatus("Redirecting to checkout...");
    const appUrl = typeof window !== "undefined" ? window.location.origin : "";
    const res = await apiFetch<{ url: string }>("/api/stripe/checkout", {
      method: "POST",
      body: JSON.stringify({
        plan,
        successUrl: \`\${appUrl}/settings/subscription?upgraded=1\`,
        cancelUrl: \`\${appUrl}/settings/subscription\`,
      }),
    });
    setActionLoading(false);
    if (res.error || !res.data?.url) {
      setActionStatus(res.error?.message ?? "Could not start checkout. Check Stripe configuration.");
      return;
    }
    window.location.href = res.data.url;
  };

  const handlePortal = async () => {
    setActionLoading(true);
    setActionStatus("Redirecting to billing portal...");
    const appUrl = typeof window !== "undefined" ? window.location.origin : "";
    const res = await apiFetch<{ url: string }>("/api/stripe/portal", {
      method: "POST",
      body: JSON.stringify({ returnUrl: \`\${appUrl}/settings/subscription\` }),
    });
    setActionLoading(false);
    if (res.error || !res.data?.url) {
      setActionStatus(res.error?.message ?? "Could not open billing portal. Check Stripe configuration.");
      return;
    }
    window.location.href = res.data.url;
  };

  const currentPlan = sub?.plan ?? "free";
  const planLabel = PLAN_LABELS[currentPlan] ?? currentPlan;
  const planDesc = PLAN_DESCRIPTIONS[currentPlan] ?? "";

  return (
    <AppShell title="Subscription" subtitle="Manage your plan and billing details.">
      <div className="max-w-2xl space-y-6">
        {/* Current plan card */}
        <div className="glass-panel rounded-2xl p-8">
          <p className="text-xs uppercase tracking-[0.2em] text-muted-foreground font-semibold">Current Plan</p>
          {loading ? (
            <div className="mt-4 h-8 w-32 bg-secondary/60 rounded animate-pulse" />
          ) : (
            <>
              <div className="mt-3 flex items-center gap-3">
                <span className="text-3xl font-bold text-foreground">{planLabel}</span>
                {sub?.status && sub.status !== "active" && sub.status !== "free" && (
                  <span className="text-xs font-medium px-2 py-1 rounded-full bg-yellow-500/10 text-yellow-500 capitalize">
                    {sub.status}
                  </span>
                )}
                {(sub?.status === "active" || currentPlan !== "free") && (
                  <span className="text-xs font-medium px-2 py-1 rounded-full bg-primary/10 text-primary">Active</span>
                )}
              </div>
              <p className="mt-2 text-sm text-muted-foreground">{planDesc}</p>
              {sub?.cancelAtPeriodEnd && (
                <p className="mt-2 text-xs text-yellow-500">Cancels at end of billing period.</p>
              )}
            </>
          )}

          {!loading && currentPlan !== "free" && (
            <button
              onClick={handlePortal}
              disabled={actionLoading}
              className="mt-6 rounded-lg border border-border bg-secondary/30 py-2 px-4 text-sm font-medium text-foreground hover:bg-secondary/60 transition-all disabled:opacity-50"
            >
              Manage Billing
            </button>
          )}
        </div>

        {/* Upgrade options */}
        {currentPlan === "free" && (
          <div className="grid gap-4 md:grid-cols-2">
            {(["pro", "elite"] as const).map((plan) => (
              <div key={plan} className="glass-panel rounded-2xl p-6 border border-border hover:border-primary/50 transition-all">
                <p className="text-lg font-bold text-foreground capitalize">{plan}</p>
                <p className="mt-1 text-sm text-muted-foreground">{PLAN_DESCRIPTIONS[plan]}</p>
                <button
                  onClick={() => handleUpgrade(plan)}
                  disabled={actionLoading}
                  className="mt-4 w-full rounded-lg bg-primary py-2 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all disabled:opacity-50"
                >
                  Upgrade to {PLAN_LABELS[plan]}
                </button>
              </div>
            ))}
          </div>
        )}

        {currentPlan === "pro" && (
          <div className="glass-panel rounded-2xl p-6 border border-border hover:border-primary/50 transition-all">
            <p className="text-lg font-bold text-foreground">Elite</p>
            <p className="mt-1 text-sm text-muted-foreground">{PLAN_DESCRIPTIONS.elite}</p>
            <button
              onClick={() => handleUpgrade("elite")}
              disabled={actionLoading}
              className="mt-4 rounded-lg bg-primary py-2 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all disabled:opacity-50"
            >
              Upgrade to Elite
            </button>
          </div>
        )}

        {actionStatus && (
          <p className="text-sm text-muted-foreground">{actionStatus}</p>
        )}
      </div>
    </AppShell>
  );
}
`);

// ─── settings/profile/page.tsx (fix stale class names) ───────────────────────
mkdirSync("app/settings/profile", { recursive: true });
writeFileSync("app/settings/profile/page.tsx", `"use client";

import { useEffect, useState } from "react";
import { AppShell } from "../../../components/app-shell";
import { apiFetch } from "../../../lib/api";

type ProfileData = {
  user: {
    id: string;
    email: string;
    fullName?: string | null;
    phone?: string | null;
    companyName?: string | null;
    addressLine1?: string | null;
    addressLine2?: string | null;
    city?: string | null;
    state?: string | null;
    postalCode?: string | null;
    country?: string | null;
  };
};

export default function ProfilePage() {
  const [status, setStatus] = useState("");
  const [isError, setIsError] = useState(false);
  const [fullName, setFullName] = useState("");
  const [phone, setPhone] = useState("");
  const [companyName, setCompanyName] = useState("");
  const [addressLine1, setAddressLine1] = useState("");
  const [addressLine2, setAddressLine2] = useState("");
  const [city, setCity] = useState("");
  const [state, setState] = useState("");
  const [postalCode, setPostalCode] = useState("");
  const [country, setCountry] = useState("");

  useEffect(() => {
    apiFetch<ProfileData>("/api/auth/me")
      .then((res) => {
        if (!res.error && res.data) {
          const u = (res.data as unknown as ProfileData).user;
          if (u) {
            setFullName(u.fullName ?? "");
            setPhone(u.phone ?? "");
            setCompanyName(u.companyName ?? "");
            setAddressLine1(u.addressLine1 ?? "");
            setAddressLine2(u.addressLine2 ?? "");
            setCity(u.city ?? "");
            setState(u.state ?? "");
            setPostalCode(u.postalCode ?? "");
            setCountry(u.country ?? "");
          }
        }
      })
      .catch(() => {});
  }, []);

  const onSubmit = async (event: React.FormEvent) => {
    event.preventDefault();
    setStatus("Saving profile...");
    setIsError(false);
    const res = await apiFetch<ProfileData>("/api/auth/profile", {
      method: "PATCH",
      body: JSON.stringify({
        fullName: fullName || undefined,
        phone: phone || undefined,
        companyName: companyName || undefined,
        addressLine1: addressLine1 || undefined,
        addressLine2: addressLine2 || undefined,
        city: city || undefined,
        state: state || undefined,
        postalCode: postalCode || undefined,
        country: country || undefined,
      }),
    });
    if (res.error) {
      setStatus(res.error.message ?? "Profile update failed.");
      setIsError(true);
      return;
    }
    setStatus("Profile saved successfully.");
  };

  const inputCls = "w-full rounded-lg border border-border bg-background/50 px-3 py-2 text-sm focus:border-primary focus:outline-none focus:ring-primary transition-all";
  const labelCls = "block text-xs font-medium text-muted-foreground mb-1 uppercase tracking-wide";

  return (
    <AppShell title="Profile" subtitle="Keep your ledger profile current for exports.">
      <div className="max-w-2xl">
        <div className="glass-panel p-8 rounded-2xl">
          <h2 className="text-xl font-bold text-foreground">Personal & Business Details</h2>
          <p className="mt-1 text-sm text-muted-foreground">
            These details appear on tax exports and CSV reports.
          </p>
          <form className="mt-6 grid gap-5 md:grid-cols-2" onSubmit={onSubmit}>
            <div className="md:col-span-2">
              <label className={labelCls}>Full name</label>
              <input className={inputCls} type="text" value={fullName} onChange={(e) => setFullName(e.target.value)} required />
            </div>
            <div>
              <label className={labelCls}>Phone</label>
              <input className={inputCls} type="tel" value={phone} onChange={(e) => setPhone(e.target.value)} />
            </div>
            <div>
              <label className={labelCls}>Company</label>
              <input className={inputCls} type="text" value={companyName} onChange={(e) => setCompanyName(e.target.value)} />
            </div>
            <div className="md:col-span-2">
              <label className={labelCls}>Address line 1</label>
              <input className={inputCls} type="text" value={addressLine1} onChange={(e) => setAddressLine1(e.target.value)} />
            </div>
            <div className="md:col-span-2">
              <label className={labelCls}>Address line 2</label>
              <input className={inputCls} type="text" value={addressLine2} onChange={(e) => setAddressLine2(e.target.value)} />
            </div>
            <div>
              <label className={labelCls}>City</label>
              <input className={inputCls} type="text" value={city} onChange={(e) => setCity(e.target.value)} />
            </div>
            <div>
              <label className={labelCls}>State</label>
              <input className={inputCls} type="text" value={state} onChange={(e) => setState(e.target.value)} />
            </div>
            <div>
              <label className={labelCls}>Postal code</label>
              <input className={inputCls} type="text" value={postalCode} onChange={(e) => setPostalCode(e.target.value)} />
            </div>
            <div>
              <label className={labelCls}>Country</label>
              <input className={inputCls} type="text" value={country} onChange={(e) => setCountry(e.target.value)} />
            </div>
            <div className="md:col-span-2">
              <button
                type="submit"
                className="w-full rounded-lg bg-primary py-2.5 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2 transition-all hover:-translate-y-0.5"
              >
                Save profile
              </button>
            </div>
          </form>
          {status && (
            <div className={\`mt-4 rounded-lg p-3 text-sm text-center \${isError ? "bg-red-500/10 border border-red-500/20 text-red-400" : "bg-accent/10 border border-accent/20"}\`}>
              {status}
            </div>
          )}
        </div>
      </div>
    </AppShell>
  );
}
`);

console.log("✅ settings/page.tsx, settings/2fa/page.tsx, settings/subscription/page.tsx, settings/profile/page.tsx written");
