package queue

import (
	"encoding/json"
	"time"
)

// JobStatus represents the status of a queue job
type JobStatus string

const (
	StatusPending    JobStatus = "pending"
	StatusInProgress JobStatus = "in_progress"
	StatusCompleted  JobStatus = "completed"
	StatusFailed     JobStatus = "failed"
	StatusRetrying   JobStatus = "retrying"
)

// DocumentScraperJob represents a document scraping task in the queue
type DocumentScraperJob struct {
	ID                 string `json:"id"`                  // Unique job ID (typically tender ID)
	NoticeID           string `json:"notice_id"`           // Notice publication ID
	NoticeURL          string `json:"notice_url"`          // Document URL to scrape
	TenderID           string `json:"tender_id,omitempty"` // Associated tender ID from MongoDB
	CreatedAt          int64  `json:"created_at"`
	UpdatedAt          int64  `json:"updated_at"`
	Attempts           int    `json:"attempts"`
	MaxRetries         int    `json:"max_retries"`
	Status             string `json:"status"`
	Error              string `json:"error,omitempty"`
	NextRetryTime      int64  `json:"next_retry_time,omitempty"`
	CompletedAt        int64  `json:"completed_at,omitempty"`
	ProcessingStartedAt int64 `json:"processing_started_at,omitempty"`
}

// NewDocumentScraperJob creates a new document scraper job
func NewDocumentScraperJob(noticeID, noticeURL, tenderID string, maxRetries int) *DocumentScraperJob {
	now := time.Now().Unix()
	return &DocumentScraperJob{
		ID:         noticeID,
		NoticeID:   noticeID,
		NoticeURL:  noticeURL,
		TenderID:   tenderID,
		CreatedAt:  now,
		UpdatedAt:  now,
		Attempts:   0,
		MaxRetries: maxRetries,
		Status:     string(StatusPending),
	}
}

// ToJSON marshals the job to JSON
func (j *DocumentScraperJob) ToJSON() (string, error) {
	data, err := json.Marshal(j)
	return string(data), err
}

// FromJSON unmarshals JSON to DocumentScraperJob
func (j *DocumentScraperJob) FromJSON(data string) error {
	return json.Unmarshal([]byte(data), j)
}

// QueueMetrics represents queue performance metrics
type QueueMetrics struct {
	TotalJobs       int64         `json:"total_jobs"`
	PendingJobs     int64         `json:"pending_jobs"`
	InProgressJobs  int64         `json:"in_progress_jobs"`
	CompletedJobs   int64         `json:"completed_jobs"`
	FailedJobs      int64         `json:"failed_jobs"`
	AverageWaitTime time.Duration `json:"average_wait_time"`
	AverageProcessTime time.Duration `json:"average_process_time"`
	UpdatedAt       int64         `json:"updated_at"`
}

// JobResult represents the result of a completed job
type JobResult struct {
	JobID         string                 `json:"job_id"`
	Status        string                 `json:"status"`
	Success       bool                   `json:"success"`
	Error         string                 `json:"error,omitempty"`
	CompletedAt   int64                  `json:"completed_at"`
	Duration      int64                  `json:"duration_ms"`
	UploadedCount int                    `json:"uploaded_count"`
	Details       map[string]interface{} `json:"details,omitempty"`
}
