package patient

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"template_blueprint/internal/database"
	"template_blueprint/pkg/database/mongo"
	"template_blueprint/pkg/models/patient"
	"time"
)

func InsertPatient(c *gin.Context) {
	local := os.Getenv("MONGODB_DEV_LOCAL")
	db := database.New(local).GetDB()
	if db == nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
		return
	}
	mongoDB := mongo.NewDatabaseService(db)
	var req *patient.Patient
	err := c.Bind(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
		return
	}

	dateCreated := time.Now().Format("2006-01-02 15:04:05")
	req.ResourceType = "Patient"
	req.CreatedAt = dateCreated

	errInsert := mongoDB.InsertPatient(req)
	if errInsert != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Patient successfully inserted"})
}

func GetAllPatient(c *gin.Context) {
	local := os.Getenv("MONGODB_DEV_LOCAL")
	db := database.New(local).GetDB()
	if db == nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
		return
	}
	mongoDB := mongo.NewDatabaseService(db)
	dataPatient, errSelect := mongoDB.GetAllDataPatient()
	if errSelect != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
		return
	}
	c.JSON(http.StatusOK, dataPatient)
}
