package ai_pipeline

import (
	"strings"

	"tm/pkg/ai_summarizer"
)

// TenderRefForm identifies one tender in batch and pipeline requests.
type TenderRefForm struct {
	ContractFolderID    string `json:"contract_folder_id" valid:"required"`
	NoticePublicationID string `json:"notice_publication_id" valid:"required"`
}

// ScrapeDocumentsForm is the request for POST /admin/v1/ai-pipeline/scrape/documents.
type ScrapeDocumentsForm struct {
	ContractFolderID    string `json:"contract_folder_id" valid:"required"`
	NoticePublicationID string `json:"notice_publication_id" valid:"required"`
}

// TenderBatchForm is the request body for batch scrape and summarize endpoints.
type TenderBatchForm struct {
	Tenders []TenderRefForm `json:"tenders" valid:"required"`
}

// TranslateBatchForm is the request body for POST /admin/v1/ai-pipeline/translate/batch.
type TranslateBatchForm struct {
	Tenders   []TenderRefForm `json:"tenders" valid:"required"`
	Languages []string        `json:"languages" valid:"required"`
}

// PipelineRunForm is the request body for POST /admin/v1/ai-pipeline/run.
type PipelineRunForm struct {
	ContractFolderID    string   `json:"contract_folder_id" valid:"required"`
	NoticePublicationID string   `json:"notice_publication_id" valid:"required"`
	Languages           []string `json:"languages" valid:"required"`
}

// PipelineRunBatchForm is the request body for POST /admin/v1/ai-pipeline/run/batch.
type PipelineRunBatchForm struct {
	Tenders   []TenderRefForm `json:"tenders" valid:"required"`
	Languages []string        `json:"languages" valid:"required"`
}

// PipelineWindowQueryForm is the query form for report and stats endpoints.
type PipelineWindowQueryForm struct {
	Window string `query:"window" valid:"optional,in(last_hour|last_24h|today|yesterday|last_7_days|last_30_days|all|daily)"`
}

func toTenderRefs(forms []TenderRefForm) []ai_summarizer.TenderRef {
	refs := make([]ai_summarizer.TenderRef, 0, len(forms))
	for _, form := range forms {
		refs = append(refs, ai_summarizer.NewTenderRef(form.ContractFolderID, form.NoticePublicationID))
	}
	return refs
}

func normalizeLanguages(languages []string) []string {
	out := make([]string, 0, len(languages))
	seen := make(map[string]struct{}, len(languages))
	for _, language := range languages {
		lang := strings.ToLower(strings.TrimSpace(language))
		if lang == "" {
			continue
		}
		if _, dup := seen[lang]; dup {
			continue
		}
		seen[lang] = struct{}{}
		out = append(out, lang)
	}
	return out
}
