version: '3.8'

# MySQL is not included – use your existing server MySQL (e.g. Webuzo).
# Set DATABASE_URL in .env to your MySQL connection string.

services:
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: nixon-backend
    env_file:
      - .env
    ports:
      - "8000:8000"
    environment:
      # Same MySQL URL as local FastAPI used (root .env); Django maps mysql+pymysql → mysql engine + PyMySQL
      - DATABASE_URL=${DATABASE_URL}
      # Xero API Credentials (REQUIRED for revenue data)
      - CLIENT_ID=${CLIENT_ID}
      - CLIENT_SECRET=${CLIENT_SECRET}
      - REFRESH_TOKEN=${REFRESH_TOKEN}
      - REDIRECT_URI=${REDIRECT_URI:-https://nixonstats.info}
      
      # Xero Tenant IDs (one per branch/organization)
      - XERO_TENANT_ID=${XERO_TENANT_ID}
      - XERO_BUSSELTON_TENANT_ID=${XERO_BUSSELTON_TENANT_ID}
      - XERO_MANDURAH_TENANT_ID=${XERO_MANDURAH_TENANT_ID}
      - XERO_MANAGEMENT_TENANT_ID=${XERO_MANAGEMENT_TENANT_ID}
      - XERO_ASSETS_TENANT_ID=${XERO_ASSETS_TENANT_ID}

      # Google Sheet sync script (same service account path as dashboard if set)
      - GOOGLE_DAILY_FIGURES_SHEETS_ID=${GOOGLE_DAILY_FIGURES_SHEETS_ID:-}
      - GOOGLE_CREDENTIALS_PATH=${GOOGLE_CREDENTIALS_PATH:-}
      
      # SimPRO API (legacy - kept for compatibility)
      - SIMPRO_BRANCH1_API_KEY=${SIMPRO_BRANCH1_API_KEY}
      - SIMPRO_BRANCH1_API_URL=https://nixonbusselton.simprosuite.com/api/v1.0
      - SIMPRO_BRANCH2_API_KEY=${SIMPRO_BRANCH2_API_KEY}
      - SIMPRO_BRANCH2_API_URL=https://nixonelectrics.simprosuite.com/api/v1.0
      - SIMPRO_BRANCH3_API_KEY=${SIMPRO_BRANCH3_API_KEY}
      - SIMPRO_BRANCH3_API_URL=https://nixonmandurah.simprosuite.com/api/v1.0
      - SYNC_INTERVAL_SECONDS=600
      - SECURE_COOKIES=${SECURE_COOKIES:-false}
      # Set true/1/yes to show maintenance UI and return 503 on API (except /health and /api/site-status)
      - SITE_UPGRADE_MODE=${SITE_UPGRADE_MODE:-}
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      # Mount tokens directory for persistent Xero token storage
      - backend-tokens:/app/data/tokens
      # Mount config directory for optional file-based config fallback / migration source
      - backend-config:/app/data/config
      # Persist uploaded media (e.g. client logos)
      - backend-media:/app/media
      - ./data:/data    
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "python", "-c", "import requests; r=requests.get('http://localhost:8000/health', timeout=2); exit(0 if r.status_code==200 else 1)"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 45s
    networks:
      - nixon-network

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: ${VITE_API_URL:-https://nixonstats.info}
        VITE_GOOGLE_SHEETS_ID: ${VITE_GOOGLE_SHEETS_ID:-}
    container_name: nixon-frontend
    ports:
      - "90:80"
    environment:
      # Origin only; frontend calls /api/overview etc. (no /api in base URL)
      - VITE_API_URL=https://nixonstats.info
    depends_on:
      backend:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - nixon-network

volumes:
  backend-tokens:
    driver: local
  backend-config:
    driver: local
  backend-media:
    driver: local

networks:
  nixon-network:
    driver: bridge

