package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"time"
)

type Response struct {
	Status  string      `json:"status"`
	Data    interface{} `json:"data"`
	Time    string      `json:"time"`
	Version string      `json:"version"`
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
	resp := Response{
		Status:  "ok",
		Data:    "service is running",
		Time:    time.Now().Format(time.RFC3339),
		Version: "1.0.0",
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

func metricsHandler(w http.ResponseWriter, r *http.Request) {
	metrics := map[string]int{
		"requests_total":  rand.Intn(10000),
		"errors_total":    rand.Intn(50),
		"active_threads":  rand.Intn(100),
		"memory_usage_mb": rand.Intn(512),
	}
	resp := Response{
		Status:  "ok",
		Data:    metrics,
		Time:    time.Now().Format(time.RFC3339),
		Version: "1.0.0",
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

func main() {
	rand.Seed(time.Now().UnixNano())
	http.HandleFunc("/health", healthHandler)
	http.HandleFunc("/metrics", metricsHandler)
	fmt.Println("Server starting on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
