# Time Tracker Backend Makefile

.PHONY: db-start db-stop db-test model-test run build clean migrate seed swagger help

# Default target
.DEFAULT_GOAL := help

# Variables
BINARY_NAME=timetracker
DB_CONTAINER=timetracker_db

# Help target
help:
	@echo "Time Tracker Backend Makefile"
	@echo ""
	@echo "Usage:"
	@echo "  make db-start    - Start the PostgreSQL database container"
	@echo "  make db-stop     - Stop the PostgreSQL database container"
	@echo "  make db-test     - Test the database connection"
	@echo "  make model-test  - Test the database models"
	@echo "  make run         - Run the application"
	@echo "  make build       - Build the application"
	@echo "  make clean       - Remove build artifacts"
	@echo "  make migrate     - Run database migrations"
	@echo "  make seed        - Seed the database with initial data"
	@echo "  make db-drop-users - Drop the users table"
	@echo "  make db-reinit   - Re-initialize the database"
	@echo "  make swagger     - Generate swagger documentation"
	@echo "  make help        - Show this help message"
	@echo "" make generate-ts - Generate TypeScript types

# Start the database
db-start:
	@echo "Starting PostgreSQL database container..."
	@cd .. && docker-compose up -d db
	@echo "Database container started"

# Stop the database
db-stop:
	@echo "Stopping PostgreSQL database container..."
	@cd .. && docker-compose stop db
	@echo "Database container stopped"

# Test the database connection
db-test:
	@echo "Testing database connection..."
	@go run cmd/dbtest/main.go

# Test the database models
model-test:
	@echo "Testing database models..."
	@go run cmd/modeltest/main.go

# Run the application
run:
	@echo "Running the application..."
	@go run cmd/api/main.go

# Build the application
build:
	@echo "Building the application..."
	@go build -o $(BINARY_NAME) cmd/api/main.go
	@echo "Build complete: $(BINARY_NAME)"

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	@rm -f $(BINARY_NAME)
	@echo "Clean complete"

# Run database migrations
migrate:
	@echo "Running database migrations..."
	@go run -mod=mod cmd/migrate/main.go
	@echo "Migrations complete"

# Seed the database with initial data
seed:
	@echo "Seeding the database..."
	@go run -mod=mod cmd/seed/main.go
	@echo "Seeding complete"

# Drop the users table
db-drop-users:
	@echo "Dropping the users table..."
	@export PG_HOST=$(DB_HOST); export PG_PORT=$(DB_PORT); export PG_USER=$(DB_USER); export PG_PASSWORD=$(DB_PASSWORD); export PG_DBNAME=$(DB_NAME); go run cmd/dbtest/main.go -drop_table=users
	@echo "Users table dropped"

# Re-initialize the database
db-reinit:
	@echo "Re-initializing the database..."
	@PG_HOST=$(DB_HOST) PG_PORT=$(DB_PORT) PG_USER=$(DB_USER) PG_PASSWORD=$(DB_PASSWORD) PG_DBNAME=$(DB_NAME) go run cmd/migrate/main.go -create_db -drop_db
# Generate swagger documentation
swagger:
	@echo "Generating swagger documentation..."
	@swag init -g cmd/api/main.go
	@echo "Swagger documentation generated"

# Generate TypeScript types
generate-ts:
	@echo "Generating TypeScript types..."
	@go run scripts/fix_tygo.go
	@echo "TypeScript types generated"

