package tender

import "tm/pkg/response"

// UpdateTenderRequest represents a request to update an existing tender
type UpdateTenderRequest struct {
	ID             string        `json:"id" valid:"required~Tender ID is required"`
	Title          *string       `json:"title,omitempty" valid:"stringlength(1|500)~Title must be between 1 and 500 characters"`
	Description    *string       `json:"description,omitempty" valid:"stringlength(0|5000)~Description must not exceed 5000 characters"`
	Status         *TenderStatus `json:"status,omitempty"`
	EstimatedValue *float64      `json:"estimated_value,omitempty" valid:"range(0|999999999999)~Estimated value must be between 0 and 999999999999"`
	Currency       *string       `json:"currency,omitempty" valid:"stringlength(3|3)~Currency must be 3 characters"`
	TenderDeadline *int64        `json:"tender_deadline,omitempty"` // Unix milliseconds
}

// ListTendersRequest represents a request to list tenders with pagination and filtering
type ListTendersRequest struct {
	Criteria  TenderSearchCriteria `json:"criteria"`
	Limit     int                  `json:"limit" valid:"range(1|100)~Limit must be between 1 and 100"`
	Offset    int                  `json:"offset" valid:"range(0|999999)~Offset must be non-negative"`
	CompanyID *string              `json:"company_id,omitempty"` // Optional company ID for match percentage calculation
	From      *int64               `json:"from,omitempty"`
	To        *int64               `json:"to,omitempty"`
}

// ListTendersResponse represents the response for listing tenders
type ListTendersResponse struct {
	Tenders  []TenderResponse `json:"tenders"`
	Metadata *response.Meta   `json:"metadata"`
}

// TenderResponse represents a tender in API responses
type TenderResponse struct {
	ID                  string                `json:"id"`
	NoticePublicationID string                `json:"notice_publication_id"`
	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"`
	PublicationDate     int64                 `json:"publication_date"`
	TenderDeadline      int64                 `json:"tender_deadline"`
	SubmissionDeadline  int64                 `json:"submission_deadline"`
	ApplicationDeadline int64                 `json:"application_deadline"`
	SubmissionURL       string                `json:"submission_url"`
	CountryCode         string                `json:"country_code"`
	BuyerOrganization   *OrganizationResponse `json:"buyer_organization"`
	Status              TenderStatus          `json:"status"`
	TenderID            string                `json:"tender_id"`
}

type OrganizationResponse struct {
	Name string `json:"name"`
}

// ToTenderResponse converts a Tender entity to TenderResponse
func (t *Tender) ToTenderResponse() *TenderResponse {
	response := &TenderResponse{
		ID:                  t.ID.Hex(),
		NoticePublicationID: t.NoticePublicationID,
		Title:               t.Title,
		Description:         t.Description,
		ProcurementTypeCode: t.ProcurementTypeCode,
		ProcedureCode:       t.ProcedureCode,
		EstimatedValue:      t.EstimatedValue,
		Currency:            t.Currency,
		Duration:            t.Duration,
		DurationUnit:        t.DurationUnit,
		PublicationDate:     t.PublicationDate,
		TenderDeadline:      t.TenderDeadline,
		SubmissionDeadline:  t.SubmissionDeadline,
		ApplicationDeadline: t.ApplicationDeadline,
		CountryCode:         t.CountryCode,
		SubmissionURL:       t.SubmissionURL,
		BuyerOrganization:   &OrganizationResponse{Name: t.BuyerOrganization.Name},
		Status:              t.Status,
		TenderID:            t.TenderID,
	}

	return response
}
