package models

import (
	"log"
)


type RaspFeedbackNossh struct {
	Id 			string 	`json:"id" gorm:"PRIMARY_KEY"`
	Addr        string `json:"addr"`
	Description string	`json:"description"`
	Complete 	bool	`json:"complete"`
}

func GetAllFeedBackListNossh() ([]*RaspFeedbackNossh,error) {
	var list []*RaspFeedbackNossh
	if err := db.Model(&RaspFeedbackNossh{}).Find(&list).Error; err != nil {
		return nil,err
	}
	return list,nil
}

func IsCompleteNossh(id string)bool{
	var feedback RaspFeedbackNossh
	err:=db.Model(&RaspFeedbackNossh{}).Where("id=?",id).First(&feedback).Error
	if err!=nil{
		log.Println(err)
		return true
	}
	//fmt.Println(feedback)
	if feedback==(RaspFeedbackNossh{}){
		return true
	}
	return feedback.Complete
}

func AddFeedBackNossh(fd RaspFeedbackNossh)bool{
	if err:=db.Create(&fd).Error;err != nil {
		db.Save(&fd)
		log.Println(err)
		return false
	}
	return true
}

func DelFeedBackNossh(id string){
	err:=db.Delete(&RaspFeedbackNossh{}).Where("id=?",id).Error
	if err!=nil{
		log.Println(err)
	}
}

func DropFeedBackNossh(){
	db.DropTable(&RaspFeedbackNossh{})
}

func CompleteNossh(fd RaspFeedbackNossh){
	db.Model(&fd).Update("Complete",true)

}

func DeleteCompleteFeedbackNossh(){
	err:=db.Where("complete=?",1).Delete(&RaspFeedbackNossh{}).Error
	if err!=nil{
		log.Println(err)
	}
}

func ExistsUncompletedFeedbackNossh(addr string) (bool,error){
	var count int64
	err := db.Model(&RaspFeedbackNossh{}).Where("addr=? and complete = 0 ",addr).Count(&count).Error
	if err != nil {
		return false,err
	}

	if count == 1 {
		return true,nil
	}
	return false,nil
}