package api

import "bit.admedia.com/scm/ad/adpilot-indexing-repo-sync.com/internal/domain"

// IndexingDiagnosticResponse is the REST representation of a durable diagnostic.
type IndexingDiagnosticResponse struct {
	DiagnosticID string `json:"diagnostic_id"`
	RepoID       string `json:"repo_id"`
	SnapshotID   string `json:"snapshot_id"`
	Stage        string `json:"stage"`
	SourceType   string `json:"source_type"`
	SourceID     string `json:"source_id"`
	JobID        string `json:"job_id,omitempty"`
	EventID      string `json:"event_id,omitempty"`
	Code         string `json:"code"`
	Severity     string `json:"severity"`
	Message      string `json:"message"`
	CreatedAt    string `json:"created_at"`
}

// IndexingDiagnosticsResponse wraps diagnostic results.
type IndexingDiagnosticsResponse struct {
	Diagnostics []IndexingDiagnosticResponse `json:"diagnostics"`
}

// IndexingDiagnosticFromDomain maps a diagnostic to an API response.
func IndexingDiagnosticFromDomain(diagnostic domain.IndexingDiagnostic) IndexingDiagnosticResponse {
	return IndexingDiagnosticResponse{
		DiagnosticID: diagnostic.DiagnosticID,
		RepoID:       diagnostic.RepoID,
		SnapshotID:   diagnostic.SnapshotID,
		Stage:        diagnostic.Stage,
		SourceType:   diagnostic.SourceType,
		SourceID:     diagnostic.SourceID,
		JobID:        diagnostic.JobID,
		EventID:      diagnostic.EventID,
		Code:         diagnostic.Code,
		Severity:     diagnostic.Severity,
		Message:      diagnostic.Message,
		CreatedAt:    diagnostic.CreatedAt.UTC().Format("2006-01-02T15:04:05Z"),
	}
}

// IndexingDiagnosticsFromDomain maps diagnostics to an API response.
func IndexingDiagnosticsFromDomain(diagnostics []domain.IndexingDiagnostic) IndexingDiagnosticsResponse {
	out := make([]IndexingDiagnosticResponse, len(diagnostics))
	for i, diagnostic := range diagnostics {
		out[i] = IndexingDiagnosticFromDomain(diagnostic)
	}
	return IndexingDiagnosticsResponse{Diagnostics: out}
}
