---
# Redis Cache Setup via Docker

- name: Create Redis data directory
  file:
    path: /var/lib/redis/data
    state: directory
    mode: '0755'

- name: Create Redis config directory
  file:
    path: /etc/redis
    state: directory
    mode: '0755'

- name: Create Redis configuration
  copy:
    dest: /etc/redis/redis.conf
    content: |
      # Redis Configuration for Undateable.me
      bind 0.0.0.0
      port {{ redis_port }}

      # Memory management
      maxmemory 2gb
      maxmemory-policy allkeys-lru

      # Persistence
      save 900 1
      save 300 10
      save 60 10000

      # Append-only file
      appendonly yes
      appendfsync everysec

      # Security
      {% if redis_password %}
      requirepass {{ redis_password }}
      {% endif %}

      # Performance
      tcp-keepalive 300
      timeout 0

      # Logging
      loglevel notice

- name: Deploy Redis container
  docker_container:
    name: redis
    image: redis:{{ redis_version }}
    state: started
    restart_policy: always
    networks:
      - name: undateable-net
    ports:
      - "{{ redis_port }}:{{ redis_port }}"
    volumes:
      - /var/lib/redis/data:/data
      - /etc/redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

- name: Wait for Redis to be ready
  wait_for:
    port: "{{ redis_port }}"
    delay: 3
    timeout: 30
