import { describe, expect, it } from "vitest";
import {
  loginSchema,
  orphanSchema,
  orphanTransferSchema,
  passwordResetSchema,
  sponsorSchema
} from "@/lib/validation";
import { normalizePhone } from "@/lib/utils";

describe("account validation", () => {
  it("normalizes common Sudanese phone formats to E.164", () => {
    expect(normalizePhone("0912 345 678")).toBe("+249912345678");
    expect(normalizePhone("00249912345678")).toBe("+249912345678");
    expect(normalizePhone("+249-912-345-678")).toBe("+249912345678");
  });

  it("accepts an admin email, local sponsor username, or international phone", () => {
    const phoneLogin = loginSchema.parse({ identifier: "0912345678", password: "password123" });
    const internationalLogin = loginSchema.parse({ identifier: "+966562063561", password: "password123" });
    const emailLogin = loginSchema.parse({ identifier: "ADMIN@example.org", password: "password123" });

    expect(phoneLogin.identifier).toEqual({ type: "username", value: "0912345678" });
    expect(internationalLogin.identifier).toEqual({ type: "phone", value: "+966562063561" });
    expect(emailLogin.identifier).toEqual({ type: "email", value: "admin@example.org" });
    expect(loginSchema.safeParse({ identifier: "not-an-account", password: "password123" }).success).toBe(false);
  });

  it("requires at least six password characters when an administrator creates a sponsor", () => {
    expect(sponsorSchema.safeParse({
      fullName: "متبرع تجريبي",
      phone: "0912345678",
      password: "123456",
      isActive: true
    }).success).toBe(true);
    expect(sponsorSchema.safeParse({
      fullName: "متبرع تجريبي",
      phone: "0912345678",
      password: "12345",
      isActive: true
    }).success).toBe(false);
  });

  it("allows an administrator to assign a matching new password to any account id", () => {
    const input = {
      id: "11111111-1111-4111-8111-111111111111",
      password: "new-password",
      confirmation: "new-password"
    };

    expect(passwordResetSchema.safeParse(input).success).toBe(true);
    expect(passwordResetSchema.safeParse({ ...input, password: "12345", confirmation: "12345" }).success).toBe(false);
    expect(passwordResetSchema.safeParse({ ...input, confirmation: "different-password" }).success).toBe(false);
  });

  it("allows an orphan case with every manual field empty", () => {
    const result = orphanSchema.parse({
      fullName: "",
      dateOfBirth: "",
      gender: "",
      sponsorNotes: "",
      privateNotes: "",
      guardianName: "",
      guardianPhone: "",
      guardianRelationship: "",
      address: "",
      bankName: "",
      bankAccountName: "",
      bankAccountNumber: "",
      monthlyNeed: ""
    });
    expect(result).toMatchObject({
      fullName: null,
      dateOfBirth: null,
      gender: null,
      guardianPhone: null,
      bankName: null,
      monthlyNeed: null
    });
  });

  it("requires both a sponsor and an orphan for a transfer", () => {
    const transfer = {
      sponsorId: "11111111-1111-4111-8111-111111111111",
      orphanId: "22222222-2222-4222-8222-222222222222",
      amount: 25_000,
      transferDate: "2026-07-26",
      reference: "",
      sponsorVisibleNotes: "",
      internalNotes: ""
    };

    expect(orphanTransferSchema.safeParse(transfer).success).toBe(true);
    expect(orphanTransferSchema.safeParse({ ...transfer, sponsorId: "" }).success).toBe(false);
    expect(orphanTransferSchema.safeParse({ ...transfer, orphanId: "" }).success).toBe(false);
  });
});
