import { prisma } from '../../config/database';
import type { PrivacySettings } from '@prisma/client';

/** Default privacy settings for new users */
const DEFAULTS: Omit<PrivacySettings, 'id' | 'userId' | 'createdAt' | 'updatedAt'> = {
  showOnlineStatus: true,
  showCurrentlyWatching: true,
  showInFriendActivity: true,
  anonymizePublicComments: false,
  hidePublicReactions: false,
  hidePublicFinishes: false,
  allowFriendRequests: true,
  closeFriendsOnlyWatching: false,
  showOnLeaderboard: true,
};

export const privacyService = {
  /** Get privacy settings (auto-create with defaults if missing) */
  async getSettings(userId: string): Promise<PrivacySettings> {
    let settings = await prisma.privacySettings.findUnique({ where: { userId } });
    if (!settings) {
      settings = await prisma.privacySettings.create({
        data: { userId, ...DEFAULTS },
      });
    }
    return settings;
  },

  /** Update privacy settings (upsert) */
  async updateSettings(userId: string, data: Partial<typeof DEFAULTS>) {
    return prisma.privacySettings.upsert({
      where: { userId },
      update: { ...data, updatedAt: new Date() },
      create: { userId, ...DEFAULTS, ...data },
    });
  },
};
