# HCFS Go Development Environment
FROM bzzz-hcfs-base:latest

LABEL maintainer="anthony@deepblack.cloud"
LABEL description="HCFS Go development environment with modern Go tools"
LABEL language="go"
LABEL version="1.0.0"

# Install Go
ENV GO_VERSION=1.21.3
RUN wget -O go.tar.gz "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" && \
    tar -C /usr/local -xzf go.tar.gz && \
    rm go.tar.gz

# Set up Go environment
ENV GOROOT=/usr/local/go
ENV GOPATH=/home/agent/go
ENV GOCACHE=/home/agent/.cache/go-build
ENV GOMODCACHE=/home/agent/.cache/go-mod
ENV PATH=$GOROOT/bin:$GOPATH/bin:$PATH

# Create Go workspace
RUN sudo -u agent mkdir -p /home/agent/go/{bin,src,pkg} && \
    sudo -u agent mkdir -p /home/agent/work/{cmd,internal,pkg,api,web,scripts,docs,tests}

# Install Go development tools
RUN sudo -u agent bash -c 'go install golang.org/x/tools/gopls@latest' && \
    sudo -u agent bash -c 'go install golang.org/x/tools/cmd/goimports@latest' && \
    sudo -u agent bash -c 'go install golang.org/x/lint/golint@latest' && \
    sudo -u agent bash -c 'go install github.com/goreleaser/goreleaser@latest' && \
    sudo -u agent bash -c 'go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest' && \
    sudo -u agent bash -c 'go install github.com/go-delve/delve/cmd/dlv@latest' && \
    sudo -u agent bash -c 'go install github.com/swaggo/swag/cmd/swag@latest' && \
    sudo -u agent bash -c 'go install github.com/air-verse/air@latest'

# Install popular Go frameworks and libraries
RUN sudo -u agent bash -c 'cd /tmp && go mod init temp && \
    go get github.com/gin-gonic/gin@latest && \
    go get github.com/gorilla/mux@latest && \
    go get github.com/echo-community/echo/v4@latest && \
    go get github.com/gofiber/fiber/v2@latest && \
    go get gorm.io/gorm@latest && \
    go get github.com/stretchr/testify@latest && \
    go get github.com/spf13/cobra@latest && \
    go get github.com/spf13/viper@latest'

# Install HCFS Go SDK
COPY hcfs-go-sdk /opt/hcfs/go-sdk
RUN cd /opt/hcfs/go-sdk && sudo -u agent go mod tidy

# Create Go project template
RUN sudo -u agent bash -c 'cat > /home/agent/work/go.mod.template << EOF
module hcfs-agent-project

go 1.21

require (
    github.com/hcfs/go-sdk v0.1.0
    github.com/gin-gonic/gin v1.9.1
    github.com/spf13/cobra v1.7.0
    github.com/spf13/viper v1.16.0
)

replace github.com/hcfs/go-sdk => /opt/hcfs/go-sdk
EOF'

RUN sudo -u agent bash -c 'cat > /home/agent/work/main.go.template << EOF
package main

import (
    "fmt"
    "log"
    
    "github.com/hcfs/go-sdk/client"
)

func main() {
    // Initialize HCFS client
    hcfsClient, err := client.NewHCFSClient("http://host.docker.internal:8000")
    if err != nil {
        log.Fatal("Failed to create HCFS client:", err)
    }
    
    fmt.Println("HCFS Go agent starting...")
    
    // Your agent code here
}
EOF'

# Create Makefile template
RUN sudo -u agent bash -c 'cat > /home/agent/work/Makefile.template << EOF
.PHONY: build run test clean lint fmt

BINARY_NAME=agent
MAIN_PATH=./cmd/main.go

build:
	go build -o bin/$(BINARY_NAME) $(MAIN_PATH)

run:
	go run $(MAIN_PATH)

test:
	go test -v ./...

test-coverage:
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out

clean:
	go clean
	rm -f bin/$(BINARY_NAME)
	rm -f coverage.out

lint:
	golangci-lint run

fmt:
	go fmt ./...
	goimports -w .

deps:
	go mod tidy
	go mod download

.DEFAULT_GOAL := build
EOF'

# Go-specific HCFS integration script
COPY scripts/go-hcfs-init.go /opt/hcfs/scripts/
RUN chmod +x /opt/hcfs/scripts/go-hcfs-init.go

# Expose common Go development ports
EXPOSE 8080 8000 9000 2345

# Add Go-specific entrypoint
COPY scripts/go-entrypoint.sh /opt/hcfs/
RUN chmod +x /opt/hcfs/go-entrypoint.sh

ENTRYPOINT ["/opt/hcfs/go-entrypoint.sh"]
CMD ["go", "version"]