version: '3.8'

services:
  database:
    image: postgres
    environment:
      - POSTGRES_DB=depot      # Matches DB_NAME in production.env
      - POSTGRES_USER=postgres # Matches DB_USER in production.env
      - POSTGRES_PASSWORD=admin # Matches DB_PASSWORD in production.env
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d depot"]  # Check specific database
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - app-network

  web:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - production.env
    depends_on:
      database:
        condition: service_healthy
    networks:
      - app-network
    command: >
      bash -c "python manage.py migrate &&
               python manage.py runserver 0.0.0.0:8000"

  minio:
    image: minio/minio:latest
    ports:
      - "9000:9000"  # API Port
      - "9001:9001"  # Console Port
    environment:
      MINIO_ROOT_USER: kikimor      # Change this
      MINIO_ROOT_PASSWORD: shushunka1
      MINIO_SERVER_URL: http://localhost:9000
      MINIO_ADDRESS: "0.0.0.0:9000"
      MINIO_BROWSER_REDIRECT_URL: "http://localhost:9001"  # Console URL
    volumes:
      - minio_data:/data
    command: server --console-address ":9001" /data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 30s
      timeout: 20s
      retries: 3
    networks:
      - app-network

  # Optional: Create buckets and users on startup
  createbuckets:
    image: minio/mc
    depends_on:
      - minio
    entrypoint: >
      /bin/sh -c "
      sleep 10;
      mc alias set myminio http://minio:9000 kikimor shushunka1;
      mc mb myminio/damages;
      mc anonymous set download myminio/damages;
      mc policy set public myminio/damages;
      echo '{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::damages/*\"]}]}' > /tmp/policy.json;
      mc admin policy add myminio getonly /tmp/policy.json;
      mc admin policy set myminio getonly user=kikimor;
      exit 0;
      "
    networks:
      - app-network

volumes:
  postgres_data:
  minio_data:
networks:
  app-network:
    driver: bridge