package page

import (
	"fmt"
	"time"

	"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/pkg/validation"
)

// Allowed page statuses
var AllowedPageStatuses = []string{"publish", "draft", "pending", "private", "trash"}

// Allowed post types
var AllowedPostTypes = []string{"post", "page", "attachment", "custom"}

// SyncPageInput represents a single page to sync in the request
type SyncPageInput struct {
	PageID      string    `json:"page_id"`
	Title       string    `json:"title"`
	Content     string    `json:"content"`
	Excerpt     string    `json:"excerpt"`
	URL         string    `json:"url"`
	Status      string    `json:"status"`
	PostType    string    `json:"post_type"`
	Author      string    `json:"author"`
	PublishedAt time.Time `json:"published_at"`
	ModifiedAt  time.Time `json:"modified_at"`
}

// Validate validates a single page input
func (p *SyncPageInput) Validate() error {
	v := validation.NewValidator()

	// Validate page ID (required)
	if err := v.ValidateRequired(p.PageID, "page_id"); err != nil {
		return err
	}
	if err := v.ValidateLength(p.PageID, "page_id", 1, 255); err != nil {
		return err
	}

	// Validate title
	title, err := v.ValidateAndSanitizeString(p.Title, "title", 1, 500)
	if err != nil {
		return err
	}
	p.Title = title

	// Validate content (optional but has max length if provided)
	if p.Content != "" {
		if err := v.ValidateLength(p.Content, "content", 0, 1000000); err != nil { // 1MB limit
			return err
		}
	}

	// Validate excerpt (optional but has max length if provided)
	if p.Excerpt != "" {
		if err := v.ValidateLength(p.Excerpt, "excerpt", 0, 1000); err != nil {
			return err
		}
	}

	// Validate URL
	if err := v.ValidateURL(p.URL, "url"); err != nil {
		return err
	}

	// Validate status (enum)
	if err := v.ValidateEnum(p.Status, "status", AllowedPageStatuses); err != nil {
		return err
	}

	// Validate post type (enum)
	if err := v.ValidateEnum(p.PostType, "post_type", AllowedPostTypes); err != nil {
		return err
	}

	// Validate author
	author, err := v.ValidateAndSanitizeString(p.Author, "author", 1, 255)
	if err != nil {
		return err
	}
	p.Author = author
	if err := v.ValidateNoHTML(p.Author, "author"); err != nil {
		return err
	}

	return nil
}

// SyncRequest represents the sync pages request
type SyncRequest struct {
	Pages []SyncPageInput `json:"pages"`
}

// Validate validates the sync request
func (r *SyncRequest) Validate() error {
	// Check pages array is not empty
	if len(r.Pages) == 0 {
		return fmt.Errorf("pages: array cannot be empty")
	}

	// Validate maximum number of pages in a single request
	if len(r.Pages) > 1000 {
		return fmt.Errorf("pages: cannot sync more than 1000 pages at once")
	}

	// Validate each page
	for i, page := range r.Pages {
		if err := page.Validate(); err != nil {
			return fmt.Errorf("pages[%d]: %w", i, err)
		}
	}

	return nil
}

// SyncResponse represents the sync pages response
type SyncResponse struct {
	SyncedCount  int      `json:"synced_count"`
	IndexedCount int      `json:"indexed_count"`
	FailedPages  []string `json:"failed_pages,omitempty"`
	Message      string   `json:"message"`
}
