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

export const PLAYER_ALLOW_PASSWORD_CHANGE = 'player.allow_password_change';
export const PLAYER_ALLOW_USERNAME_CHANGE = 'player.allow_username_change';
export const AGENT_SUSPEND_FREEZE_DIRECT_PLAYERS = 'agent.suspend_freeze_direct_players';
export const AGENT_SUSPEND_BLOCK_PLAYER_LOGIN = 'agent.suspend_block_player_login';

export type PlayerAccountSettings = {
  allowPasswordChange: boolean;
  allowUsernameChange: boolean;
};

export type AgentSuspendSettings = {
  /** 停用代理时是否允许级联冻结其直属玩家（需管理员显式勾选） */
  suspendFreezeDirectPlayers: boolean;
  /** 上级代理停用时是否禁止其直属玩家登录 */
  suspendBlockPlayerLogin: boolean;
};

@Injectable()
export class SystemConfigService {
  constructor(private prisma: PrismaService) {}

  async getBoolean(key: string, defaultValue: boolean): Promise<boolean> {
    const row = await this.prisma.systemConfig.findUnique({ where: { configKey: key } });
    if (!row) return defaultValue;
    return row.configValue === 'true' || row.configValue === '1';
  }

  async setBoolean(key: string, value: boolean, description?: string) {
    await this.prisma.systemConfig.upsert({
      where: { configKey: key },
      create: {
        configKey: key,
        configValue: value ? 'true' : 'false',
        description,
      },
      update: { configValue: value ? 'true' : 'false' },
    });
  }

  async getPlayerAccountSettings(): Promise<PlayerAccountSettings> {
    const [allowPasswordChange, allowUsernameChange] = await Promise.all([
      this.getBoolean(PLAYER_ALLOW_PASSWORD_CHANGE, true),
      this.getBoolean(PLAYER_ALLOW_USERNAME_CHANGE, false),
    ]);
    return { allowPasswordChange, allowUsernameChange };
  }

  async updatePlayerAccountSettings(data: Partial<PlayerAccountSettings>) {
    if (data.allowPasswordChange !== undefined) {
      await this.setBoolean(
        PLAYER_ALLOW_PASSWORD_CHANGE,
        data.allowPasswordChange,
        '玩家是否可在客户端修改密码',
      );
    }
    if (data.allowUsernameChange !== undefined) {
      await this.setBoolean(
        PLAYER_ALLOW_USERNAME_CHANGE,
        data.allowUsernameChange,
        '玩家是否可在客户端修改登录账号名',
      );
    }
    return this.getPlayerAccountSettings();
  }

  async getAgentSuspendSettings(): Promise<AgentSuspendSettings> {
    const [suspendFreezeDirectPlayers, suspendBlockPlayerLogin] = await Promise.all([
      this.getBoolean(AGENT_SUSPEND_FREEZE_DIRECT_PLAYERS, false),
      this.getBoolean(AGENT_SUSPEND_BLOCK_PLAYER_LOGIN, false),
    ]);
    return { suspendFreezeDirectPlayers, suspendBlockPlayerLogin };
  }

  async updateAgentSuspendSettings(data: Partial<AgentSuspendSettings>) {
    if (data.suspendFreezeDirectPlayers !== undefined) {
      await this.setBoolean(
        AGENT_SUSPEND_FREEZE_DIRECT_PLAYERS,
        data.suspendFreezeDirectPlayers,
        '停用代理时是否允许级联冻结直属玩家',
      );
    }
    if (data.suspendBlockPlayerLogin !== undefined) {
      await this.setBoolean(
        AGENT_SUSPEND_BLOCK_PLAYER_LOGIN,
        data.suspendBlockPlayerLogin,
        '上级代理停用时是否禁止直属玩家登录',
      );
    }
    return this.getAgentSuspendSettings();
  }
}
