import { prisma } from "../database";
import { UserSettings } from "@prisma/client";

export interface CreateUserSettingsData {
  userId: string;
  defaultQuality?: string;
  publicProfile?: boolean;
  allowDownload?: boolean;
}

export interface UpdateUserSettingsData {
  defaultQuality?: string;
  publicProfile?: boolean;
  allowDownload?: boolean;
}

export class UserSettingsService {
  /**
   * 获取用户设置
   */
  static async getUserSettings(userId: string): Promise<UserSettings | null> {
    try {
      const settings = await prisma.userSettings.findUnique({
        where: { userId },
      });

      return settings;
    } catch (error) {
      console.error("Failed to get user settings:", error);
      throw error;
    }
  }

  /**
   * 创建用户设置
   */
  static async createUserSettings(
    data: CreateUserSettingsData
  ): Promise<UserSettings> {
    try {
      const settings = await prisma.userSettings.create({
        data: {
          userId: data.userId,
          defaultQuality: data.defaultQuality ?? "medium",
          publicProfile: data.publicProfile ?? false,
          allowDownload: data.allowDownload ?? true,
        },
      });

      return settings;
    } catch (error) {
      console.error("Failed to create user settings:", error);
      throw error;
    }
  }

  /**
   * 更新用户设置
   */
  static async updateUserSettings(
    userId: string,
    data: UpdateUserSettingsData
  ): Promise<UserSettings> {
    try {
      // 检查用户设置是否存在
      let settings = await prisma.userSettings.findUnique({
        where: { userId },
      });

      if (!settings) {
        // 如果不存在，创建默认设置
        settings = await this.createUserSettings({
          userId,
          ...data,
        });
      } else {
        // 更新现有设置
        settings = await prisma.userSettings.update({
          where: { userId },
          data,
        });
      }

      return settings;
    } catch (error) {
      console.error("Failed to update user settings:", error);
      throw error;
    }
  }

  /**
   * 删除用户设置
   */
  static async deleteUserSettings(userId: string): Promise<void> {
    try {
      await prisma.userSettings.delete({
        where: { userId },
      });
    } catch (error) {
      console.error("Failed to delete user settings:", error);
      throw error;
    }
  }

  /**
   * 获取或创建用户设置
   */
  static async getOrCreateUserSettings(userId: string): Promise<UserSettings> {
    try {
      let settings = await this.getUserSettings(userId);

      if (!settings) {
        settings = await this.createUserSettings({
          userId,
        });
      }

      return settings;
    } catch (error) {
      console.error("Failed to get or create user settings:", error);
      throw error;
    }
  }

  /**
   * 重置用户设置为默认值
   */
  static async resetUserSettings(userId: string): Promise<UserSettings> {
    try {
      const settings = await prisma.userSettings.upsert({
        where: { userId },
        update: {
          defaultQuality: "medium",
          publicProfile: false,
          allowDownload: true,
        },
        create: {
          userId,
          defaultQuality: "medium",
          publicProfile: false,
          allowDownload: true,
        },
      });

      return settings;
    } catch (error) {
      console.error("Failed to reset user settings:", error);
      throw error;
    }
  }
}
