stages:
  - build
  - deploy

variables:
  DOCKER_REGISTRY: "docker.io"
  IMAGE_NAME: "mikeintosh/derejame" # Change this to your desired image name
  COMPOSE_PROJECT_NAME: "derejame"
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""            # <– disables TLS so DinD works
  DOCKER_HOST: tcp://docker:2375    # <– tells Docker CLI where to connect  

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .cache/docker

before_script:
#  - docker info
  - |
    for i in {1..10}; do
      docker info && break
      echo "Waiting for Docker daemon..."
      sleep 3
    done  

build-app:
  stage: build
  image: docker:24.0.7
  services:
    - docker:24.0.7-dind
  script:
    - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
    - |
      docker build \
        -t $DOCKER_REGISTRY/$IMAGE_NAME:latest \
        -t $DOCKER_REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA \
        --label "maintainer=MIKEINTOSH SYSTEMS <mikeintoshsys@gmail.com>" \
        --label "tags=derejame" \
        .
    - docker push $DOCKER_REGISTRY/$IMAGE_NAME:latest
    - docker push $DOCKER_REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA

build-chatbot:
  stage: build
  image: docker:24.0.7
  services:
    - docker:24.0.7-dind
  script:
    - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
    - |
      cd ai/chat && \
      docker build \
        -t $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:latest \
        -t $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:$CI_COMMIT_SHORT_SHA \
        --label "maintainer=MIKEINTOSH SYSTEMS <mikeintoshsys@gmail.com>" \
        --label "tags=derejame,chatbot" \
        . && \
      cd ../..
    - docker push $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:latest
    - docker push $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:$CI_COMMIT_SHORT_SHA

deploy-production:
  stage: deploy
  image: docker:24.0.7
  services:
    - docker:24.0.7-dind
  tags:
#    - derejame  # Must match your runner's tags Development runner tag
    - derejaproduction  # Production runner tag
  script:
    - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
    - docker-compose version
    
    # Pull the specific versioned images
    - docker pull $DOCKER_REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
    - docker pull $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:$CI_COMMIT_SHORT_SHA
    
    # Tag them appropriately for compose
    - docker tag $DOCKER_REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA ${COMPOSE_PROJECT_NAME}_php
    - docker tag $DOCKER_REGISTRY/$IMAGE_NAME-chatbot:$CI_COMMIT_SHORT_SHA ${COMPOSE_PROJECT_NAME}_chat
    
    # Stop and recreate containers
    - docker-compose -f docker-compose.yml -p $COMPOSE_PROJECT_NAME down
    - docker-compose -f docker-compose.yml pull
    - docker-compose -f docker-compose.yml -p $COMPOSE_PROJECT_NAME up -d --build
    #- docker exec -i $COMPOSE_PROJECT_NAME_mariadb mariadb -u root -pmerqderejadmin -e "GRANT ALL PRIVILEGES ON *.* TO 'me_dereja'@'%' IDENTIFIED BY 'me_dereja' WITH GRANT OPTION; FLUSH PRIVILEGES;"
    
    # Cleanup
    - docker image prune -f
  only:
    - main  # Only deploy when changes are pushed to main branch
  environment:
    name: production
    url: http://mne.dereja.com