# Firefly III - Personal Finance Manager
# https://docs.firefly-iii.org/how-to/firefly-iii/installation/docker/

services:
  firefly-redis:
    image: redis:alpine
    hostname: firefly-redis
    container_name: firefly_iii_redis
    restart: unless-stopped
    volumes:
      - firefly_redis_data:/data
    networks:
      - firefly-network
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"

  firefly:
    image: fireflyiii/core:latest
    hostname: firefly
    container_name: firefly_iii_core
    restart: unless-stopped
    ports:
      - "8094:8080"
    volumes:
      - /home/icke/firefly/upload:/var/www/html/storage/upload
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    environment:
      # Application settings
      - APP_KEY=pxhYxHRvrt84ocbN42vy8Tn9CcmP5EnC
      - APP_ENV=local
      - APP_DEBUG=false
      - SITE_OWNER=admin@example.com
      - APP_URL=http://localhost:8094
      - TRUSTED_PROXIES=**
      
      # Database settings (MariaDB)
      - DB_CONNECTION=mysql
      - DB_HOST=firefly-db
      - DB_PORT=3306
      - DB_DATABASE=firefly
      - DB_USERNAME=firefly
      - DB_PASSWORD=firefly_secure_password_123
      
      # Cache settings - OPTIMIZED with Redis
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - REDIS_HOST=firefly-redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=
      - REDIS_DB=0
      - REDIS_CACHE_DB=1
      
      # Email settings (optional - configure for notifications)
      # - MAIL_MAILER=smtp
      # - MAIL_HOST=smtp.example.com
      # - MAIL_PORT=587
      # - MAIL_FROM=noreply@example.com
      # - MAIL_USERNAME=your_email@example.com
      # - MAIL_PASSWORD=your_email_password
      # - MAIL_ENCRYPTION=tls
      
      # Localization
      - DEFAULT_LANGUAGE=en_US
      - DEFAULT_LOCALE=equal
      - TZ=Europe/Berlin
      
      # Security
      - AUTHENTICATION_GUARD=web
      - AUTHENTICATION_GUARD_EMAIL=email
      
      # Webhooks
      - ALLOW_WEBHOOKS=true
      
      # Cron Job Token (must be exactly 32 characters)
      - STATIC_CRON_TOKEN=oVFRBboIBlZPRgc5HYHA5Hevrg68VaTV
      
      # Other settings
      - LOG_CHANNEL=stack
      - APP_LOG_LEVEL=info
      - AUDIT_LOG_LEVEL=info
      
    depends_on:
      - firefly-db
      - firefly-redis
    networks:
      - firefly-network
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"

  firefly-db:
    image: mariadb:lts
    hostname: firefly-db
    container_name: firefly_iii_db
    restart: unless-stopped
    command: --innodb-buffer-pool-size=512M --innodb-log-file-size=128M --max-connections=100
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_DATABASE=firefly
      - MYSQL_USER=firefly
      - MYSQL_PASSWORD=firefly_secure_password_123
    volumes:
      - /home/icke/firefly/db:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - firefly-network
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"

  firefly-cron:
    # Cron job container for automated tasks
    # Set STATIC_CRON_TOKEN in environment - must be exactly 32 characters
    image: alpine
    hostname: firefly-cron
    container_name: firefly_iii_cron
    restart: unless-stopped
    environment:
      - TZ=Europe/Berlin
      - STATIC_CRON_TOKEN=oVFRBboIBlZPRgc5HYHA5Hevrg68VaTV
    command: >-
      sh -c "apk add --no-cache tzdata wget curl && 
      ln -fs /usr/share/zoneinfo/$$TZ /etc/localtime && 
      echo 'Starting Firefly III cron job scheduler...' && 
      echo \"0 3 * * * wget -qO- http://firefly:8080/api/v1/cron/$$STATIC_CRON_TOKEN 2>&1 | logger -t firefly-cron\" | crontab - && 
      echo 'Cron job installed. Next run: 03:00 daily' && 
      crond -f -L /dev/stdout"
    networks:
      - firefly-network
    depends_on:
      - firefly
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"

networks:
  firefly-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.50.0.0/28

volumes:
  firefly_upload:
  firefly_db:
  firefly_redis_data:
