version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: mold_cost_tool
    ports:
      - "5002:5002"
    env_file:
      - .env
    environment:
      - FLASK_APP=run.py
      - FLASK_ENV=production
      - LOG_LEVEL=INFO
      - LOG_TO_STDOUT=True
    volumes:
      - ./logs:/app/logs
      - ./instance:/app/instance
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - mold_network

  db:
    image: docker.io/library/postgres:14-alpine
    container_name: mold_cost_db
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=mold_cost
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./deploy/db/init:/docker-entrypoint-initdb.d
    ports:
      - "5433:5432"
    restart: unless-stopped
    networks:
      - mold_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  backup:
    image: docker.io/library/postgres:14-alpine
    container_name: mold_cost_backup
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=mold_cost
    volumes:
      - ./backups:/backups
      - ./deploy/db/backup.sh:/backup.sh
      - ./deploy/db/restore.sh:/restore.sh
    depends_on:
      db:
        condition: service_healthy
    networks:
      - mold_network
    command: >
      sh -c "
        chmod +x /backup.sh /restore.sh &&
        echo 'Starting backup service...' &&
        echo 'Creating initial backup...' &&
        /backup.sh &&
        echo 'Setting up daily backups at 2 AM...' &&
        while true; do
          # Wait until 2 AM
          current_hour=\$(date +%H)
          if [ \$current_hour -eq 2 ]; then
            echo 'Creating scheduled daily backup...' &&
            /backup.sh
          fi
          # Sleep for 1 hour and check again
          sleep 3600
        done
      "
    restart: unless-stopped

volumes:
  postgres_data:
    name: mold_cost_postgres_data

networks:
  mold_network:
    name: mold_cost_network 