/**
 * Manual mock for @/lib/redis — Upstash Redis client
 * Used in Jest tests to avoid real Redis connections
 *
 * Exports match lib/redis.ts:
 *   - redis (client instance with common Redis methods)
 *   - redisConfigured (boolean)
 *   - checkRedisHealth (async fn)
 */

// ---------------------------------------------------------------------------
// Pipeline / Multi mock (returns itself, exec resolves to empty array)
// ---------------------------------------------------------------------------
function makePipelineMock() {
  const pipeline = {
    get: jest.fn().mockReturnThis(),
    set: jest.fn().mockReturnThis(),
    del: jest.fn().mockReturnThis(),
    exists: jest.fn().mockReturnThis(),
    expire: jest.fn().mockReturnThis(),
    setex: jest.fn().mockReturnThis(),
    incr: jest.fn().mockReturnThis(),
    decr: jest.fn().mockReturnThis(),
    hget: jest.fn().mockReturnThis(),
    hset: jest.fn().mockReturnThis(),
    hdel: jest.fn().mockReturnThis(),
    hgetall: jest.fn().mockReturnThis(),
    lpush: jest.fn().mockReturnThis(),
    rpush: jest.fn().mockReturnThis(),
    lrange: jest.fn().mockReturnThis(),
    sadd: jest.fn().mockReturnThis(),
    srem: jest.fn().mockReturnThis(),
    smembers: jest.fn().mockReturnThis(),
    zadd: jest.fn().mockReturnThis(),
    zrange: jest.fn().mockReturnThis(),
    zrem: jest.fn().mockReturnThis(),
    exec: jest.fn().mockResolvedValue([]),
  };
  return pipeline;
}

// ---------------------------------------------------------------------------
// Redis client mock
// ---------------------------------------------------------------------------
const redis = {
  // String commands
  get: jest.fn().mockResolvedValue(null),
  set: jest.fn().mockResolvedValue("OK"),
  del: jest.fn().mockResolvedValue(1),
  exists: jest.fn().mockResolvedValue(0),
  expire: jest.fn().mockResolvedValue(1),
  setex: jest.fn().mockResolvedValue("OK"),
  setnx: jest.fn().mockResolvedValue(1),
  mget: jest.fn().mockResolvedValue([]),
  mset: jest.fn().mockResolvedValue("OK"),

  // Numeric commands
  incr: jest.fn().mockResolvedValue(1),
  decr: jest.fn().mockResolvedValue(0),
  incrby: jest.fn().mockResolvedValue(1),
  decrby: jest.fn().mockResolvedValue(0),

  // Key commands
  keys: jest.fn().mockResolvedValue([]),
  ttl: jest.fn().mockResolvedValue(-1),
  type: jest.fn().mockResolvedValue("none"),
  rename: jest.fn().mockResolvedValue("OK"),
  scan: jest.fn().mockResolvedValue([0, []]),

  // Hash commands
  hget: jest.fn().mockResolvedValue(null),
  hset: jest.fn().mockResolvedValue(1),
  hdel: jest.fn().mockResolvedValue(1),
  hgetall: jest.fn().mockResolvedValue(null),
  hmset: jest.fn().mockResolvedValue("OK"),
  hmget: jest.fn().mockResolvedValue([]),
  hincrby: jest.fn().mockResolvedValue(1),
  hexists: jest.fn().mockResolvedValue(0),
  hkeys: jest.fn().mockResolvedValue([]),
  hvals: jest.fn().mockResolvedValue([]),
  hlen: jest.fn().mockResolvedValue(0),

  // List commands
  lpush: jest.fn().mockResolvedValue(1),
  rpush: jest.fn().mockResolvedValue(1),
  lpop: jest.fn().mockResolvedValue(null),
  rpop: jest.fn().mockResolvedValue(null),
  lrange: jest.fn().mockResolvedValue([]),
  llen: jest.fn().mockResolvedValue(0),
  lrem: jest.fn().mockResolvedValue(0),

  // Set commands
  sadd: jest.fn().mockResolvedValue(1),
  srem: jest.fn().mockResolvedValue(1),
  smembers: jest.fn().mockResolvedValue([]),
  sismember: jest.fn().mockResolvedValue(0),
  scard: jest.fn().mockResolvedValue(0),

  // Sorted set commands
  zadd: jest.fn().mockResolvedValue(1),
  zrange: jest.fn().mockResolvedValue([]),
  zrem: jest.fn().mockResolvedValue(1),
  zscore: jest.fn().mockResolvedValue(null),
  zcard: jest.fn().mockResolvedValue(0),
  zrangebyscore: jest.fn().mockResolvedValue([]),

  // Pipeline / Multi
  pipeline: jest.fn(() => makePipelineMock()),
  multi: jest.fn(() => makePipelineMock()),

  // Utility
  ping: jest.fn().mockResolvedValue("PONG"),
  eval: jest.fn().mockResolvedValue(null),
  evalsha: jest.fn().mockResolvedValue(null),
  flushdb: jest.fn().mockResolvedValue("OK"),
  flushall: jest.fn().mockResolvedValue("OK"),
  dbsize: jest.fn().mockResolvedValue(0),
  info: jest.fn().mockResolvedValue(""),
  quit: jest.fn().mockResolvedValue("OK"),
  disconnect: jest.fn().mockResolvedValue(undefined),
};

// ---------------------------------------------------------------------------
// Exports — match lib/redis.ts shape
// ---------------------------------------------------------------------------
const redisConfigured = false;

async function checkRedisHealth() {
  return false;
}

module.exports = {
  redis,
  redisConfigured,
  checkRedisHealth,
  default: redis,
};
