import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';

// Admin settings stored in memory (could be moved to database later)
@Injectable()
export class AdminSettingsService {
  private settings: Record<string, any> = {
    // Basic settings
    systemName: '幼儿阅读教学服务平台',
    systemDesc: '',
    contactPhone: '',
    contactEmail: '',
    systemLogo: '',

    // Security settings
    passwordStrength: 'medium',
    maxLoginAttempts: 5,
    tokenExpire: '7d',
    forceHttps: false,

    // Notification settings
    emailEnabled: true,
    smtpHost: '',
    smtpPort: 465,
    smtpUser: '',
    smtpPassword: '',
    fromEmail: '',
    smsEnabled: false,

    // Storage settings
    storageType: 'local',
    maxFileSize: 100,
    allowedTypes: '.jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.ppt,.pptx',

    // Tenant defaults
    defaultTeacherQuota: 20,
    defaultStudentQuota: 200,
    enableAutoExpire: true,
    notifyBeforeDays: 30,
  };

  constructor(private prisma: PrismaService) {}

  async getSettings() {
    return { ...this.settings };
  }

  async getSetting(key: string) {
    return this.settings[key];
  }

  async updateSettings(data: Record<string, any>) {
    // Update only provided keys
    for (const key of Object.keys(data)) {
      if (key in this.settings) {
        this.settings[key] = data[key];
      }
    }
    return { ...this.settings };
  }

  async getBasicSettings() {
    return {
      systemName: this.settings.systemName,
      systemDesc: this.settings.systemDesc,
      contactPhone: this.settings.contactPhone,
      contactEmail: this.settings.contactEmail,
      systemLogo: this.settings.systemLogo,
    };
  }

  async getSecuritySettings() {
    return {
      passwordStrength: this.settings.passwordStrength,
      maxLoginAttempts: this.settings.maxLoginAttempts,
      tokenExpire: this.settings.tokenExpire,
      forceHttps: this.settings.forceHttps,
    };
  }

  async getNotificationSettings() {
    return {
      emailEnabled: this.settings.emailEnabled,
      smtpHost: this.settings.smtpHost,
      smtpPort: this.settings.smtpPort,
      fromEmail: this.settings.fromEmail,
      smsEnabled: this.settings.smsEnabled,
    };
  }

  async getStorageSettings() {
    return {
      type: this.settings.storageType,
      maxFileSize: this.settings.maxFileSize,
      allowedTypes: this.settings.allowedTypes,
    };
  }

  async getTenantDefaults() {
    return {
      defaultTeacherQuota: this.settings.defaultTeacherQuota,
      defaultStudentQuota: this.settings.defaultStudentQuota,
      enableAutoExpire: this.settings.enableAutoExpire,
      notifyBeforeDays: this.settings.notifyBeforeDays,
    };
  }
}
