package feedback

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

// ListFeedbackForm represents the form for listing feedback
type ListFeedbackForm struct {
	Feedbacks []FeedbackResponse `json:"feedbacks"`
	DateFrom  *int64             `json:"date_from,omitempty" validate:"omitempty,min=0"`
	DateTo    *int64             `json:"date_to,omitempty" validate:"omitempty,min=0"`
	Limit     int                `json:"limit" validate:"required,min=1,max=100"`
	Offset    int                `json:"offset" validate:"required,min=0"`
}

// ToggleFeedbackForm represents the form for toggling feedback (like/dislike)
type ToggleFeedbackForm struct {
	TenderID     string       `json:"tender_id" validate:"required"`
	FeedbackType FeedbackType `json:"feedback_type" validate:"required,oneof=like dislike"`
}

// FeedbackResponse represents the response for feedback operations
type FeedbackResponse struct {
	ID           string       `json:"id"`
	FeedbackType FeedbackType `json:"feedback_type"`
	UpdatedAt    int64        `json:"updated_at"`
	CreatedAt    int64        `json:"created_at"`
	TenderID     string       `json:"tender"`
	CustomerID   *string      `json:"customer_id"`
	CompanyId    *string      `json:"company_id"`
}

// FeedbackListResponse represents a paginated list of feedback
type FeedbackListResponse struct {
	Feedback []*FeedbackResponse `json:"feedback"`
	Meta     *response.Meta      `json:"meta"`
}

// FeedbackListResponse represents a paginated list of feedback
type FeedbackListWithTenderResponse struct {
	Feedback []*FeedbackWithTenderResponse `json:"feedback"`
	Meta     *response.Meta                `json:"meta"`
}
type FeedbackWithTenderResponse struct {
	ID           string         `json:"id"`
	FeedbackType FeedbackType   `json:"feedback_type"`
	UpdatedAt    int64          `json:"updated_at"`
	CreatedAt    int64          `json:"created_at"`
	TenderID     string         `json:"tender_id"`
	CustomerID   *string        `json:"customer_id"`
	CompanyId    *string        `json:"company_id"`
	Tender       *tenderDetails `json:"tender"`
}

// tenderDetails represents essential tender information for the response
type (
	tenderDetails struct {
		ID                  string                `json:"id"`
		NoticePublicationID string                `json:"notice_publication_id"`
		PublicationDate     int64                 `json:"publication_date"`
		Title               string                `json:"title"`
		Description         string                `json:"description"`
		ProcurementTypeCode string                `json:"procurement_type_code"`
		ProcedureCode       string                `json:"procedure_code"`
		EstimatedValue      float64               `json:"estimated_value"`
		Currency            string                `json:"currency"`
		Duration            string                `json:"duration"`
		DurationUnit        string                `json:"duration_unit"`
		TenderDeadline      int64                 `json:"tender_deadline"`
		SubmissionDeadline  int64                 `json:"submission_deadline"`
		CountryCode         string                `json:"country_code"`
		BuyerOrganization   *organizationResponse `json:"buyer_organization"`
		Status              string                `json:"status"`
	}
	organizationResponse struct {
		Name string `json:"name"`
	}
)

// CompanyFeedbackStatsResponse represents the response for company feedback statistics
type CompanyFeedbackStatsResponse struct {
	CompanyID     string `json:"company_id"`
	TotalLikes    int64  `json:"total_likes"`
	TotalDislikes int64  `json:"total_dislikes"`
	TotalFeedback int64  `json:"total_feedback"`
	LastUpdated   int64  `json:"last_updated"` // Unix timestamp
}

// GetDateFromTime returns the date_from as time.Time
func (f *ListFeedbackForm) GetDateFromTime() *time.Time {
	if f.DateFrom == nil {
		return nil
	}
	t := time.Unix(*f.DateFrom, 0)
	return &t
}

// GetDateToTime returns the date_to as time.Time
func (f *ListFeedbackForm) GetDateToTime() *time.Time {
	if f.DateTo == nil {
		return nil
	}
	t := time.Unix(*f.DateTo, 0)
	return &t
}

func (f *Feedback) ToResponse() *FeedbackResponse {
	return &FeedbackResponse{
		ID:           f.ID.Hex(),
		FeedbackType: f.FeedbackType,
		TenderID:     f.TenderID,
		CustomerID:   f.CustomerID,
		CompanyId:    f.CompanyID,
		UpdatedAt:    f.UpdatedAt,
		CreatedAt:    f.CreatedAt,
	}
}

func (f *Feedback) ToResponseWithTender(tender *tenderDetails) *FeedbackWithTenderResponse {
	return &FeedbackWithTenderResponse{
		ID:           f.ID.Hex(),
		FeedbackType: f.FeedbackType,
		TenderID:     f.TenderID,
		CustomerID:   f.CustomerID,
		CompanyId:    f.CompanyID,
		UpdatedAt:    f.UpdatedAt,
		CreatedAt:    f.CreatedAt,
		Tender:       tender,
	}
}
