services:
  dev-postgres:
    image: postgres:17
    container_name: dev-postgres
    environment:
      POSTGRES_DB: devdb
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: devpassword
    ports:
      - "7433:5432"
    command:
      - bash
      - -c
      - |
        mkdir -p /wal-queue && chown postgres:postgres /wal-queue
        exec docker-entrypoint.sh postgres \
          -c wal_level=replica \
          -c max_wal_senders=3 \
          -c archive_mode=on \
          -c "archive_command=cp %p /wal-queue/%f"
    volumes:
      - ./wal-queue:/wal-queue
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U devuser -d devdb"]
      interval: 2s
      timeout: 5s
      retries: 30

  db-writer:
    image: postgres:17
    container_name: dev-db-writer
    depends_on:
      dev-postgres:
        condition: service_healthy
    environment:
      PGHOST: dev-postgres
      PGPORT: "5432"
      PGUSER: devuser
      PGPASSWORD: devpassword
      PGDATABASE: devdb
    command:
      - bash
      - -c
      - |
        echo "Waiting for postgres..."
        until pg_isready -h dev-postgres -U devuser -d devdb; do sleep 1; done

        psql -c "DROP TABLE IF EXISTS wal_generator;"
        psql -c "CREATE TABLE wal_generator (id SERIAL PRIMARY KEY, data TEXT NOT NULL);"
        echo "Starting WAL generation loop..."
        while true; do
          echo "Inserting ~50MB of data..."
          psql -c "INSERT INTO wal_generator (data) SELECT repeat(md5(random()::text), 640) FROM generate_series(1, 2500);"
          echo "Deleting data..."
          psql -c "DELETE FROM wal_generator;"
          echo "Cycle complete, sleeping 5s..."
          sleep 5
        done
