version: 2
jobs:
    test-backend:
        docker:
            - image: cimg/node:14.20
            - image: cimg/mariadb:10.8
              environment:
                  MYSQL_ALLOW_EMPTY_PASSWORD: "true"
                  MYSQL_DATABASE: photos_test
                  MYSQL_USER: photos
                  MYSQL_PASSWORD: photos
                  MYSQL_HOST: "localhost"

        working_directory: ~/photos

        steps:
            - checkout

            - restore_cache:
                  keys:
                      - backend-dependencies-{{ checksum "package.json" }}

            - run:
                  name: install backend deps
                  command: npm i

            - save_cache:
                  paths:
                      - node_modules
                  key: backend-dependencies-{{ checksum "package.json" }}

            - run:
                  name: test backend
                  command: npm test

            - store_test_results:
                  path: backend-report.xml

    test-frontend:
        docker:
            - image: cimg/node:14.20

        working_directory: ~/photos

        steps:
            - checkout
            - restore_cache:
                  keys:
                      - frontend-dependencies-{{ checksum "frontend/package.json" }}

            - run:
                  name: install frontend deps
                  command: cd frontend && npm i

            - save_cache:
                  paths:
                      - frontend/node_modules
                  key: frontend-dependencies-{{ checksum "frontend/package.json" }}

            - run:
                  name: test frontend
                  command: cd frontend && npm test

            - store_test_results:
                  path: ~/photos/frontend/frontend-reports/frontend-report.xml

    test-frontend-build:
        docker:
            - image: cimg/node:14.20

        working_directory: ~/photos

        steps:
            - checkout:

            - restore_cache:
                  keys:
                      - frontend-dependencies-{{ checksum "frontend/package.json" }}

            - run:
                  name: install frontend deps
                  command: cd frontend && npm i

            - save_cache:
                  paths:
                      - frontend/node_modules
                  key: frontend-dependencies-{{ checksum "frontend/package.json" }}

            - run:
                  name: build frontend
                  command: cd frontend && npm run build
    build:
        machine:
            image: ubuntu-2004:current
        working_directory: ~/photos
        resource_class: large

        steps:
            - checkout
            - run:
                  name: log in to docker hub
                  command: echo "$DOCKER_PASSWORD" | docker login --username $DOCKER_USERNAME --password-stdin

            - run:
                  name: install emulators
                  command: docker run --privileged --rm tonistiigi/binfmt --install all

            - run:
                  name: create docker builder
                  command: docker buildx create --use --driver=docker-container

            - restore_cache:
                  keys:
                      - buildx-photos-circleci-

            - run:
                  name: build and push to docker hub
                  command: |
                      docker buildx build --progress=plain --push --platform linux/arm64,linux/amd64 --tag stepanusatiuk/photos:$CIRCLE_BRANCH \
                      --cache-to=type=local,mode=max,dest=/tmp/dockercache \
                      --cache-from=type=local,src=/tmp/dockercache .
            - run:
                  name: prune cache
                  command: docker buildx prune --keep-storage=2gb --verbose

            - save_cache:
                  key: buildx-photos-circleci-{{ checksum "/tmp/dockercache/index.json" }}
                  paths:
                      - /tmp/dockercache

#    build-arm:
#        machine:
#            image: ubuntu-2004:current
#        resource_class: arm.medium
#        working_directory: ~/photos
#
#        steps:
#            - checkout
#            - run:
#                  name: log in to docker hub
#                  command: echo "$DOCKER_PASSWORD" | docker login --username $DOCKER_USERNAME --password-stdin
#
#            - run:
#                  name: install emulators
#                  command: docker run --privileged --rm tonistiigi/binfmt --install all
#
#            - run:
#                  name: create docker builder
#                  command: docker buildx create --use
#
#            - run:
#                  name: build and push to docker hub
#                  #command: docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag stepanusatiuk/photos:latest .
#                  command: docker buildx build --push --platform linux/arm64 --tag stepanusatiuk/photos:$CIRCLE_BRANCH .

workflows:
    version: 2
    test-build:
        jobs:
            - test-backend
            - test-frontend
            - test-frontend-build:
                  requires:
                      - test-frontend
            - build:
                  requires:
                      - test-backend
                      - test-frontend
                      - test-frontend-build
                  filters:
                      branches:
                          only: main
#            - build-arm:
#                  requires:
#                      - test-backend
#                      - test-frontend
#                      - test-frontend-build
