import fs from "node:fs";
import path from "node:path";
import { describe, expect, it } from "vitest";

function source(relativePath: string): string {
  return fs.readFileSync(path.join(process.cwd(), relativePath), "utf8");
}

describe("administrator account password resets", () => {
  const action = source("src/app/actions/admin.ts");
  const settings = source("src/app/admin/settings/page.tsx");
  const donorDetails = source("src/app/admin/donors/[id]/page.tsx");

  it("uses the authenticated admin API without requesting the old password", () => {
    expect(action).toContain("export async function resetAccountPasswordAction");
    expect(action).toContain("await adminContext()");
    expect(action).toContain("supabase.auth.admin.updateUserById");
    expect(action).toContain('action: "account.password_reset"');
    expect(action).not.toContain('formData.get("oldPassword")');
  });

  it("offers the reset action for all profiles and keeps it on donor details", () => {
    expect(settings).toContain('from("profiles")');
    expect(settings).toContain("إعادة تعيين كلمة مرور أي حساب");
    expect(settings).toContain("دون الحاجة إلى معرفة كلمة المرور القديمة");
    expect(settings).toContain("action={resetAccountPasswordAction}");
    expect(donorDetails).toContain("action={resetAccountPasswordAction}");
    expect(donorDetails).toContain('name="returnTo"');
  });
});

describe("donor lifetime contributions", () => {
  const dashboard = source("src/app/donor/page.tsx");

  it("loads the donor-scoped aggregate and displays it on the dashboard", () => {
    expect(dashboard).toContain('supabase.rpc("get_my_total_contributions")');
    expect(dashboard).toContain('label="إجمالي تبرعاتك"');
    expect(dashboard).toContain("formatSdg(totalContributions)");
  });
});
