include .env
.PHONY: test
test:
	@docker compose up -d test
	@docker compose exec test go test ./...
	@docker compose stop test

.PHONY: coverage
coverage:
	@mkdir -p coverage
	@docker compose up -d test
	@docker compose exec test sh -c "go test -coverprofile=coverage.out ./internal/... && go tool cover -func=coverage.out -o coverage/coverage.txt"
	@docker cp $(shell docker ps -q -f "name=yimaru-test-1"):/app/coverage ./ || true
	@docker compose stop test

.PHONY: build
build:
	@docker compose build app

.PHONY: run
run:
	@docker compose up

.PHONY: stop
stop:
	@docker compose down

.PHONY: air
air:
	@echo "Running air locally (not in Docker)"
	@air -c .air.toml
.PHONY: migrations/new
migrations/new:
	@echo 'Creating migration files for DB_URL'
	@migrate create -seq -ext=.sql -dir=./db/migrations $(name)

.PHONY: migrations/up
migrations/up:
	@echo 'Running up migrations...'
	@docker compose up migrate

.PHONY: postgres 
postgres:
	@echo 'Running postgres db...'
	docker compose -f docker-compose.yml exec postgres psql -U root -d gh
.PHONY: backup	
backup:
	@mkdir -p backup
	@docker exec -t yimaru-backend-postgres-1 pg_dump -U root --data-only --exclude-table=schema_migrations gh | gzip > backup/dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql.gz

restore:
	@echo "Restoring latest backup..."
	@latest_file=$$(ls -t backup/dump_*.sql.gz | head -n 1); \
	echo "Restoring from $$latest_file"; \
	gunzip -c $$latest_file | docker exec -i yimaru-backend-postgres-1 psql -U root -d gh
restore_file:
	@echo "Restoring latest backup..."
	gunzip -c $(file) | docker exec -i yimaru-backend-postgres-1 psql -U root -d gh

.PHONY: seed_data
seed_data:
	
	@echo "Waiting for PostgreSQL to be ready..."
	@until docker exec yimaru-backend-postgres-1 pg_isready -U root -d gh; do \
		echo "PostgreSQL is not ready yet..."; \
		sleep 1; \
	done
	@for file in db/data/*.sql; do \
		echo "Seeding $$file..."; \
		cat $$file | docker exec -i yimaru-backend-postgres-1 psql -U root -d gh; \
	done
.PHONY: seed_dev_data
seed_dev_data:
	@echo "Waiting for PostgreSQL to be ready..."
	@until docker exec yimaru-backend-postgres-1 pg_isready -U root -d gh; do \
		echo "PostgreSQL is not ready yet..."; \
		sleep 1; \
	done
	cat db/scripts/fix_autoincrement_desync.sql | docker exec -i yimaru-backend-postgres-1 psql -U root -d gh;
	@for file in db/dev_data/*.sql; do \
		if [ -f "$$file" ]; then \
			echo "Seeding $$file..."; \
			cat $$file | docker exec -i yimaru-backend-postgres-1 psql -U root -d gh; \
		fi \
	done
postgres_log:
	docker logs yimaru-backend-postgres-1
.PHONY: swagger
swagger:
	@swag init -g cmd/main.go
.PHONY: db-up
logs:
	@mkdir -p logs
db-up: | logs
	@mkdir -p logs	
	@docker compose up -d postgres migrate mongo
	@docker logs yimaru-backend-postgres-1 > logs/postgres.log 2>&1 &
.PHONY: db-down
db-down:
	@docker compose down -v
# 	@docker volume rm yimaru-backend_postgres_data
.PHONY: sqlc-gen
sqlc-gen:
	@sqlc generate
app_log:
	@mkdir -p app_logs
export_logs: | app_log
	@docker exec yimaru-mongo mongoexport --db=logdb --collection=applogs -u root -p secret --authenticationDatabase=admin --out - > app_logs/log_`date +%Y-%m-%d"_"%H_%M_%S`.json
