import { UserSettingsService } from "@/lib/services/user-settings.service";

// Mock Prisma
jest.mock("@/lib/database", () => ({
  prisma: {
    userSettings: {
      findUnique: jest.fn(),
      create: jest.fn(),
      update: jest.fn(),
      delete: jest.fn(),
      upsert: jest.fn(),
    },
  },
}));

describe("UserSettingsService", () => {
  const mockPrisma = require("@/lib/database").prisma;

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should get user settings", async () => {
    const mockSettings = {
      id: "settings-1",
      userId: "user-1",
      defaultQuality: "medium",
      publicProfile: false,
      allowDownload: true,
      createdAt: new Date(),
      updatedAt: new Date(),
    };

    mockPrisma.userSettings.findUnique.mockResolvedValue(mockSettings);

    const result = await UserSettingsService.getUserSettings("user-1");

    expect(result).toEqual(mockSettings);
    expect(mockPrisma.userSettings.findUnique).toHaveBeenCalledWith({
      where: { userId: "user-1" },
    });
  });

  it("should return existing user settings when they exist", async () => {
    const mockSettings = {
      id: "settings-1",
      userId: "user-1",
      defaultQuality: "medium",
      publicProfile: false,
      allowDownload: true,
      createdAt: new Date(),
      updatedAt: new Date(),
    };

    mockPrisma.userSettings.findUnique.mockResolvedValue(mockSettings);

    const result = await UserSettingsService.getOrCreateUserSettings("user-1");

    expect(result).toEqual(mockSettings);
    expect(mockPrisma.userSettings.findUnique).toHaveBeenCalledWith({
      where: { userId: "user-1" },
    });
  });
});
