pg_pass := "password"
pg_user := "postgres"
pg_container := "postgres-frostbyte" # This is the name of the postgres container
pg_port := "5432"
network := "fb_network"
db_name := "frostbyte" # This is the name of the database

conn_string := "postgres://" + pg_user + ":" + pg_pass + "@" + pg_container + ":" + pg_port / db_name
conn_local := "postgres://" + pg_user + ":" + pg_pass + "@" + "localhost" + ":" + pg_port / db_name
env_string := "DATABASE_URL=" + conn_string
env_local := "DATABASE_URL=" + conn_local

# Builds a debug container and runs it
dev: start-debug
    @echo "Cd into client and run 'npm run dev' to start the client in dev mode."

# Builds a debug container
[private]
build-container-server-debug:
    podman build -t fb-server-debug -f container/ContainerfileDebug .

# Builds a debug container and runs it
[private]
start-debug: start-postgres-dev clean-podman init-sqlx build-container-server-debug 
    podman network create {{network}} --ignore
    podman run -d --network {{network}} -e {{env_string}} -p 8080:8080 --name frostbyte-debug fb-server-debug
    podman ps | grep frostbyte-debug
    @echo "Debug server started."

# Builds a release container
[private]
build-container-release: 
    podman build -t fb-server -f container/Containerfile .

# Builds a release container and runs it
start-release: start-postgres-dev clean-podman init-sqlx build-container-release create-network
    podman run -d --network {{network}} -e {{env_string}} -p 8080:8080 --name frostbyte fb-server

# Initializes the database, runs migrations and then prepares sqlx
[private]
init-sqlx: install-sqlx
    echo {{env_local}} > server/.env
    cd server && sqlx database create --connect-timeout 40 # Postgres takes a while to start up
    cd server && sqlx migrate run
    cd server && cargo sqlx prepare # If this fails, try running just clean

# Shorthand for installing sqlx
[private]
install-sqlx:
    cargo install sqlx-cli

# Starts a postgres container for development
[private]
start-postgres-dev: create-network
    podman rm -f {{pg_container}}
    podman run --network {{network}} --name {{pg_container}} -e POSTGRES_PASSWORD={{pg_pass}} -d -p {{pg_port}}:5432 docker.io/postgres:16.1-alpine

pgshell:
    podman exec -it {{pg_container}} psql -U {{pg_user}} -d {{db_name}}

re-migrate:
    echo {{env_local}} > server/.env
    cd server && cargo sqlx database drop -y
    cd server && cargo sqlx database create
    cd server && cargo sqlx migrate run
    @echo "Database re-initialized and migrations re-run."

db-backup:
    podman exec -t {{pg_container}} pg_dump -U {{pg_user}} -d {{db_name}} | gzip > frostbyte_backup$(date +'%Y-%m-%d_%H:%M:%S').sql.gz

# Drops the database and restores it from a backup file
db-restore backupfile: db-backup
    podman exec -i {{pg_container}} psql -U {{pg_user}} -d {{db_name}} -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
    gunzip -c {{backupfile}} | podman exec -i {{pg_container}} psql -U {{pg_user}} -d {{db_name}}

[private]
create-network:
    podman network create {{network}} --ignore

# Forcefully stops and removes the frostbyte container
[private]
clean-podman:
    podman container rm -f frostbyte
    podman container rm -f frostbyte-debug

# Removes the database container
[private]
clean-db:
    podman container rm -f {{pg_container}}

# Removes the network
[private]
clean-network:
    podman network rm -f {{network}}

# Forcefully removes the frostbyte images
[private]
clean-images:
    podman image rm -f fb-server
    podman image rm -f fb-server-debug
    podman image rm -f postgres
    podman image prune -af

# Cleans up everything related to the project
clean: clean-podman clean-db clean-images clean-network && state
    rm -rf client-solid/dist
    rm -rf client-solid/node_modules
    rm -rf server/public
    rm -rf server/target
    @echo "Cleaned up! Make sure to clean up podman volumes and networks."

state: 
    podman ps -a
    podman images ls -a
    podman network ls
    du -sch client* server