# List this menu and exit
help:
    just --justfile "{{ justfile_directory() }}/application/justfile" --list

# Format the app
format:
    #!/usr/bin/env amber
    main(args) {
        cd "{{ justfile_directory() }}"
        echo "=====  Formatting  ====="
        $ fourmolu -qi \
            --no-cabal \
            -o -XDefaultSignatures \
            -o -XDeriveGeneric \
            -o -XDeriveAnyClass \
            -o -XFlexibleContexts \
            -o -XFlexibleInstances \
            -o -XGADTs \
            -o -XInstanceSigs \
            -o -XLambdaCase \
            -o -XMultiParamTypeClasses \
            -o -XMultiWayIf \
            -o -XOverloadedLabels \
            -o -XOverloadedLists \
            -o -XOverloadedRecordDot \
            -o -XOverloadedStrings \
            -o -XPatternSynonyms \
            -o -XStandaloneDeriving \
            -o -XTemplateHaskell \
            -o -XTupleSections \
            -o -XTypeApplications \
            -o -XTypeFamilies \
            -o -XTypeSynonymInstances \
            -o -XViewPatterns \
            "{{ justfile_directory() }}/application" \
            > /dev/null $ failed {
            echo "Failed to run fourmolu: {status}"
            fail
        }
        $ stylish-haskell -ir \
            "{{ justfile_directory() }}/application" \
            > /dev/null $ failed {
            echo "Failed to run stylish-haskell: {status}"
            fail
        }
        $ cabal-fmt --inplace "{{ justfile_directory() }}/webserver.cabal" \
            > /dev/null $ failed {
            echo "Failed to run cabal-fmt: {status}"
            fail
        }
        $ nixfmt "{{ justfile_directory() }}/shell.nix" \
            "{{ justfile_directory() }}/nix/oci-image.nix" \
            > /dev/null $ failed {
            echo "Failed to run nixfmt: {status}"
            fail
        }
        $ just -q --unstable --fmt \
            > /dev/null $ failed {
            echo "Failed to run just: {status}"
            fail
        }
        echo "Done"
        exit 0
    }

# Format, then build the app
build: format
    #!/usr/bin/env amber
    main(args) {
        cd "{{ justfile_directory() }}"
        echo "===== Building App ====="
        $ dotenvx run --env-file {{ justfile_directory() }}/.env.development -- cabal build webserver $ failed {
            echo "Failed to build webserver: {status}"
            fail
        }
        echo "Done"
        exit 0
    }

# Format, build, then run the app
run: build
    #!/usr/bin/env amber
    main(args) {
        cd "{{ justfile_directory() }}"
        echo "=====   Starting   ====="
        trust $ dotenvx run --env-file \
            {{ justfile_directory() }}/.env.development \
            -- watchexec -r -e hs \
            -- cabal run webserver $
        exit 0
    }

# Build OCI image for production
oci-build:
    nix-build {{ justfile_directory() }}/nix/oci-image.nix -o result-image

# Load OCI image into docker
oci-load: oci-build
    docker load < result-image
