// src/common/redis/redis.service.ts
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { createClient, RedisClientType } from 'redis';

@Injectable()
export class RedisService implements OnModuleInit, OnModuleDestroy {
  private client: RedisClientType;

  async onModuleInit() {
    this.client = createClient({ url: process.env.REDIS_URL });
    await this.client.connect();
  }

  async onModuleDestroy() {
    await this.client.disconnect();
  }

  async sadd(key: string, value: string) {
    await this.client.sAdd(key, value);
  }

  async srem(key: string, value: string) {
    await this.client.sRem(key, value);
  }

  async smembers(key: string): Promise<string[]> {
    return this.client.sMembers(key);
  }

  async sismember(key: string, value: string): Promise<boolean> {
    const result = await this.client.sIsMember(key, value);
    return result === 1;
  }

  async setex(key: string, seconds: number, value: string) {
    await this.client.setEx(key, seconds, value);
  }

  async get(key: string): Promise<string | null> {
    return this.client.get(key);
  }

  async del(key: string) {
    await this.client.del(key);
  }
}
