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
}