import { describe, expect, it } from "vitest";

import {
  FacebookIdentityResponse,
  FacebookPsidLoginResponse,
  LoginRequest,
} from "@/data/dto/auth";
import { UserSchema } from "@/data/schemas/user/user";

describe("facebook identity DTO/schema", () => {
  it("parses facebook identity binding responses", () => {
    const response = FacebookIdentityResponse.fromJson({
      fbAsid: "asid-1",
      fbPsid: "psid-1",
      facebookBinding: {
        conflicts: [],
        bound: [{ field: "psid" }],
      },
    });

    expect(response.toJson()).toMatchObject({
      fbAsid: "asid-1",
      fbPsid: "psid-1",
      facebookBinding: {
        conflicts: [],
        bound: [{ field: "psid" }],
      },
    });
  });

  it("parses facebook psid login responses for real users and guests", () => {
    expect(
      FacebookPsidLoginResponse.fromJson({
        token: "login-token",
        refreshToken: "refresh-token",
        matchedBy: "psid",
        fbAsid: "asid-1",
        fbPsid: "psid-1",
        hasCompleteFacebookIdentity: true,
        isGuest: false,
        user: { id: "user-1", username: "Elio" },
      }).toJson(),
    ).toMatchObject({
      isGuest: false,
      hasCompleteFacebookIdentity: true,
      user: { id: "user-1" },
    });

    expect(
      FacebookPsidLoginResponse.fromJson({
        token: "guest-token",
        matchedBy: "guest",
        fbPsid: "psid-1",
        hasCompleteFacebookIdentity: false,
        isGuest: true,
        userId: "guest-1",
      }).toJson(),
    ).toMatchObject({
      refreshToken: "",
      isGuest: true,
      userId: "guest-1",
      user: null,
    });
  });

  it("keeps psid in request serialization without requiring DTO field declarations", () => {
    const request = LoginRequest.from({
      email: "user@example.com",
      password: "password",
      platform: "web",
      guestId: "",
      psid: "psid-1",
    });

    expect(request.toJson()).toMatchObject({ psid: "psid-1" });
  });

  it("accepts facebook identity fields in user schema", () => {
    expect(
      UserSchema.parse({
        id: "user-1",
        username: "Elio",
        fbAsid: "asid-1",
        fbPsid: "psid-1",
      }),
    ).toMatchObject({
      fbAsid: "asid-1",
      fbPsid: "psid-1",
    });
  });
});
