package controllers

import (
	"ems-backend/models"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func GetInspections(c *gin.Context) {
	var inspections []models.InspectionLedger
	// In a real app, you might want to Preload("Device") and Preload("Inspector")
	// Since we don't have complex relationships set up in GORM structs yet (just IDs), we return raw data
	if err := models.DB.Find(&inspections).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, inspections)
}

func CreateInspection(c *gin.Context) {
	var inspection models.InspectionLedger
	if err := c.ShouldBindJSON(&inspection); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	
	inspection.CheckTime = time.Now()
	// In real app, get InspectorID from Context/Token
	
	if err := models.DB.Create(&inspection).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, inspection)
}

