package feedback

import (
	"time"
	"tm/pkg/mongo"
)

// FeedbackType represents the type of feedback
type FeedbackType string

const (
	FeedbackTypeLike    FeedbackType = "like"
	FeedbackTypeDislike FeedbackType = "dislike"
)

// Feedback represents feedback given to a tender by a user
type Feedback struct {
	mongo.Model  `bson:",inline"`
	TenderID     string       `bson:"tender_id" json:"tender_id" validate:"required"`
	CustomerID   *string      `bson:"customer_id,omitempty" json:"customer_id,omitempty"`
	CompanyID    *string      `bson:"company_id,omitempty" json:"company_id,omitempty"`
	FeedbackType FeedbackType `bson:"feedback_type" json:"feedback_type" validate:"required,oneof=like dislike"`
}

// FeedbackSummary represents aggregated feedback statistics for a tender
type FeedbackSummary struct {
	TenderID      string `bson:"tender_id" json:"tender_id"`
	TotalLikes    int64  `bson:"total_likes" json:"total_likes"`
	TotalDislikes int64  `bson:"total_dislikes" json:"total_dislikes"`
	LastUpdated   int64  `bson:"last_updated" json:"last_updated"` // Unix timestamp
}

// FeedbackSearchCriteria represents search criteria for feedback
type FeedbackSearchCriteria struct {
	TenderID     *string       `json:"tender_id,omitempty"`
	CustomerID   *string       `json:"customer_id,omitempty"`
	CompanyID    *string       `json:"company_id,omitempty"`
	FeedbackType *FeedbackType `json:"feedback_type,omitempty"`
	DateFrom     *int64        `json:"date_from,omitempty"` // Unix timestamp
	DateTo       *int64        `json:"date_to,omitempty"`   // Unix timestamp
	Limit        int           `json:"limit,omitempty"`
	Offset       int           `json:"offset,omitempty"`
}

// GetID returns the feedback ID
func (f *Feedback) GetID() string {
	return f.ID.Hex()
}

// IsLike returns true if the feedback is a like
func (f *Feedback) IsLike() bool {
	return f.FeedbackType == FeedbackTypeLike
}

// IsDislike returns true if the feedback is a dislike
func (f *Feedback) IsDislike() bool {
	return f.FeedbackType == FeedbackTypeDislike
}

// UpdateFeedbackType updates the feedback type and updated timestamp
func (f *Feedback) UpdateFeedbackType(feedbackType FeedbackType) {
	f.FeedbackType = feedbackType
	f.UpdatedAt = time.Now().Unix()
}
