package scraper

import (
	"context"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"net/http"
	"strings"
	"time"

	"github.com/yourorg/devour/pkg/dockerdocs"
)

type DockerDocsScraper struct {
	config *Config
	parser *dockerdocs.Parser
	client *http.Client
}

func NewDockerDocsScraper(config *Config) *DockerDocsScraper {
	return &DockerDocsScraper{
		config: config,
		parser: dockerdocs.NewParser(),
		client: &http.Client{
			Timeout: config.Timeout,
		},
	}
}

func (s *DockerDocsScraper) Scrape(ctx context.Context, source *Source) ([]*Document, error) {
	var documents []*Document

	if source.URL == "" {
		return nil, fmt.Errorf("URL is required for Docker docs scraper")
	}

	html, err := s.fetchPage(ctx, source.URL)
	if err != nil {
		return nil, fmt.Errorf("failed to fetch page: %w", err)
	}

	page, err := s.parser.ParsePage(html, source.URL)
	if err != nil {
		return nil, fmt.Errorf("failed to parse Docker docs page: %w", err)
	}

	mainDoc := s.pageToDocument(page, source.Name)
	documents = append(documents, mainDoc)

	for _, section := range page.Sections {
		doc := s.sectionToDocument(section, page, source.Name)
		documents = append(documents, doc)
	}

	return documents, nil
}

func (s *DockerDocsScraper) DetectChanges(ctx context.Context, source *Source, lastHash string) (bool, string, error) {
	html, err := s.fetchPage(ctx, source.URL)
	if err != nil {
		return false, "", err
	}

	hash := s.generateHash(html)
	changed := hash != lastHash

	return changed, hash, nil
}

func (s *DockerDocsScraper) fetchPage(ctx context.Context, url string) (string, error) {
	return fetchExternalPage(ctx, s.client, s.config.UserAgent, url)
}

func (s *DockerDocsScraper) generateHash(content string) string {
	hash := sha256.Sum256([]byte(content))
	return hex.EncodeToString(hash[:])
}

func (s *DockerDocsScraper) pageToDocument(page *dockerdocs.Page, sourceName string) *Document {
	var content strings.Builder
	fmt.Fprintf(&content, "# %s\n\n", page.Title)
	fmt.Fprintf(&content, "%s\n", page.Description)

	if len(page.CodeBlocks) > 0 {
		fmt.Fprintf(&content, "\n## Code Examples\n")
		for _, cb := range page.CodeBlocks {
			fmt.Fprintf(&content, "\n```%s\n%s\n```\n", cb.Language, cb.Code)
		}
	}

	metadata := map[string]interface{}{
		"title":    page.Title,
		"doc_url":  page.URL,
		"doc_type": "docker-docs",
	}

	return &Document{
		ID:        generateDocID(page.URL),
		Source:    sourceName,
		Type:      "docker-docs",
		Title:     page.Title,
		Content:   content.String(),
		URL:       page.URL,
		Metadata:  metadata,
		Hash:      s.generateHash(content.String()),
		Timestamp: time.Now(),
	}
}

func (s *DockerDocsScraper) sectionToDocument(section *dockerdocs.Section, page *dockerdocs.Page, sourceName string) *Document {
	var content strings.Builder
	fmt.Fprintf(&content, "# %s\n\n", section.Title)
	fmt.Fprintf(&content, "%s\n", section.Content)

	metadata := map[string]interface{}{
		"page_title": page.Title,
		"section_id": section.ID,
		"doc_url":    section.DocURL,
		"doc_type":   "docker-section",
	}

	return &Document{
		ID:        generateDocID(section.DocURL),
		Source:    sourceName,
		Type:      "docker-section",
		Title:     fmt.Sprintf("%s - %s", page.Title, section.Title),
		Content:   content.String(),
		URL:       section.DocURL,
		Metadata:  metadata,
		Hash:      s.generateHash(content.String()),
		Timestamp: time.Now(),
	}
}
