package admin

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/gothinkster/golang-gin-realworld-example-app/common"
	"github.com/gothinkster/golang-gin-realworld-example-app/models"
)

func ListAssetAuditReports(c *gin.Context) {
	db := common.GetDB()
	p := ParsePagination(c)

	query := db.Model(&models.AssetAuditReport{})
	if v := c.Query("asset_id"); v != "" {
		query = query.Where("asset_id = ?", v)
	}
	if v := c.Query("report_type"); v != "" {
		query = query.Where("report_type = ?", v)
	}
	if v := c.Query("auditor_name"); v != "" {
		query = query.Where("auditor_name LIKE ?", "%"+v+"%")
	}
	if v := c.Query("is_active"); v != "" {
		query = query.Where("is_active = ?", v == "true")
	}

	var total int64
	query.Count(&total)

	var items []models.AssetAuditReport
	if err := query.Order("asset_id ASC, display_order ASC, id DESC").Offset(p.Offset()).Limit(p.PageSize).Find(&items).Error; err != nil {
		Fail(c, http.StatusInternalServerError, "Failed to fetch audit reports")
		return
	}
	OKList(c, items, total)
}

func GetAssetAuditReport(c *gin.Context) {
	db := common.GetDB()
	var item models.AssetAuditReport
	if err := db.First(&item, c.Param("id")).Error; err != nil {
		Fail(c, http.StatusNotFound, "Audit report not found")
		return
	}
	OK(c, item)
}

func CreateAssetAuditReport(c *gin.Context) {
	db := common.GetDB()
	var item models.AssetAuditReport
	if err := BindJSONFlexTime(c, &item); err != nil {
		Fail(c, http.StatusBadRequest, err.Error())
		return
	}
	item.ID = 0
	now := time.Now()
	if item.CreatedAt.IsZero() {
		item.CreatedAt = now
	}
	item.UpdatedAt = now
	if err := db.Create(&item).Error; err != nil {
		Fail(c, http.StatusInternalServerError, err.Error())
		return
	}
	LogOp(c, "create", "asset_audit_report", "create_audit_report", &item.ID, item)
	OK(c, item)
}

func UpdateAssetAuditReport(c *gin.Context) {
	db := common.GetDB()
	var existing models.AssetAuditReport
	if err := db.First(&existing, c.Param("id")).Error; err != nil {
		Fail(c, http.StatusNotFound, "Audit report not found")
		return
	}
	var item models.AssetAuditReport
	if err := BindJSONFlexTime(c, &item); err != nil {
		Fail(c, http.StatusBadRequest, err.Error())
		return
	}
	item.ID = existing.ID
	if existing.CreatedAt.IsZero() {
		item.CreatedAt = time.Now()
	} else {
		item.CreatedAt = existing.CreatedAt
	}
	if err := db.Save(&item).Error; err != nil {
		Fail(c, http.StatusInternalServerError, err.Error())
		return
	}
	LogOp(c, "update", "asset_audit_report", "update_audit_report", &item.ID, item)
	OK(c, item)
}

func DeleteAssetAuditReport(c *gin.Context) {
	db := common.GetDB()
	var item models.AssetAuditReport
	if err := db.First(&item, c.Param("id")).Error; err != nil {
		Fail(c, http.StatusNotFound, "Audit report not found")
		return
	}
	if err := db.Delete(&item).Error; err != nil {
		Fail(c, http.StatusInternalServerError, err.Error())
		return
	}
	LogOp(c, "delete", "asset_audit_report", "delete_audit_report", &item.ID, nil)
	OK(c, gin.H{"message": "deleted"})
}
