import { Injectable, Inject } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisService {
  constructor(
    @Inject('REDIS_CLIENT')
    private readonly redis: Redis,
  ) {}

  // 基础操作
  async get(key: string): Promise<string | null> {
    return this.redis.get(key);
  }

  async set(key: string, value: string, ttl?: number): Promise<void> {
    if (ttl) {
      await this.redis.setex(key, ttl, value);
    } else {
      await this.redis.set(key, value);
    }
  }

  async del(key: string): Promise<void> {
    await this.redis.del(key);
  }

  async exists(key: string): Promise<boolean> {
    const result = await this.redis.exists(key);
    return result === 1;
  }

  async expire(key: string, seconds: number): Promise<void> {
    await this.redis.expire(key, seconds);
  }

  // Hash操作
  async hget(key: string, field: string): Promise<string | null> {
    return this.redis.hget(key, field);
  }

  async hset(key: string, field: string, value: string): Promise<void> {
    await this.redis.hset(key, field, value);
  }

  async hgetall(key: string): Promise<Record<string, string>> {
    return this.redis.hgetall(key);
  }

  async hdel(key: string, field: string): Promise<void> {
    await this.redis.hdel(key, field);
  }

  // List操作
  async lpush(key: string, value: string): Promise<void> {
    await this.redis.lpush(key, value);
  }

  async rpush(key: string, value: string): Promise<void> {
    await this.redis.rpush(key, value);
  }

  async lrange(key: string, start: number, stop: number): Promise<string[]> {
    return this.redis.lrange(key, start, stop);
  }

  // Sorted Set操作
  async zadd(key: string, score: number, member: string): Promise<void> {
    await this.redis.zadd(key, score, member);
  }

  async zrange(key: string, start: number, stop: number): Promise<string[]> {
    return this.redis.zrange(key, start, stop);
  }

  async zrevrange(key: string, start: number, stop: number): Promise<string[]> {
    return this.redis.zrevrange(key, start, stop);
  }

  async zremrangebyscore(key: string, min: number, max: number): Promise<void> {
    await this.redis.zremrangebyscore(key, min, max);
  }

  // 行情数据专用方法
  async setTick(symbol: string, data: Record<string, string>, ttl: number = 60): Promise<void> {
    const key = `market:tick:${symbol}`;
    await this.redis.hset(key, data);
    await this.redis.expire(key, ttl);
  }

  async getTick(symbol: string): Promise<Record<string, string>> {
    const key = `market:tick:${symbol}`;
    return this.redis.hgetall(key);
  }

  async addKline(symbol: string, period: string, data: string, maxCount: number = 500): Promise<void> {
    const key = `market:kline:${symbol}:${period}`;
    const score = Date.now();
    await this.redis.zadd(key, score, data);
    await this.redis.zremrangebyrank(key, 0, -(maxCount + 1));
    await this.redis.expire(key, 7 * 24 * 60 * 60);
  }

  async getKlines(symbol: string, period: string, count: number = 100): Promise<string[]> {
    const key = `market:kline:${symbol}:${period}`;
    return this.redis.zrevrange(key, 0, count - 1);
  }

  // Session管理
  async setSession(token: string, userId: number, ttl: number): Promise<void> {
    const key = `session:${token}`;
    await this.redis.setex(key, ttl, userId.toString());
  }

  async deleteSession(token: string): Promise<void> {
    const key = `session:${token}`;
    await this.redis.del(key);
  }
}
