// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: MIT

package v2

import (
	"net/http"

	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/modules/git"
	pages_module "code.gitea.io/gitea/modules/pages"
	"code.gitea.io/gitea/services/context"
	pages_service "code.gitea.io/gitea/services/pages"
)

// PagesConfigResponse represents the pages configuration for a repository
type PagesConfigResponse struct {
	Enabled       bool                        `json:"enabled"`
	PublicLanding bool                        `json:"public_landing"`
	Template      string                      `json:"template"`
	Domain        string                      `json:"domain,omitempty"`
	Branding      pages_module.BrandingConfig `json:"branding"`
	Hero          pages_module.HeroConfig     `json:"hero"`
	SEO           pages_module.SEOConfig      `json:"seo"`
	Footer        pages_module.FooterConfig   `json:"footer"`
}

// PagesContentResponse represents the rendered content for a landing page
type PagesContentResponse struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Readme      string `json:"readme,omitempty"`
}

// GetPagesConfig returns the pages configuration for a repository
// GET /api/v2/repos/{owner}/{repo}/pages/config
func GetPagesConfig(ctx *context.APIContext) {
	repo := ctx.Repo.Repository
	if repo == nil {
		ctx.APIErrorNotFound("Repository not found")
		return
	}

	config, err := pages_service.GetPagesConfig(ctx, repo)
	if err != nil {
		ctx.APIErrorNotFound("Pages not configured")
		return
	}

	response := &PagesConfigResponse{
		Enabled:       config.Enabled,
		PublicLanding: config.PublicLanding,
		Template:      config.Template,
		Domain:        config.Domain,
		Branding:      config.Branding,
		Hero:          config.Hero,
		SEO:           config.SEO,
		Footer:        config.Footer,
	}

	ctx.JSON(http.StatusOK, response)
}

// GetPagesContent returns the rendered content for a repository's landing page
// GET /api/v2/repos/{owner}/{repo}/pages/content
func GetPagesContent(ctx *context.APIContext) {
	repo := ctx.Repo.Repository
	if repo == nil {
		ctx.APIErrorNotFound("Repository not found")
		return
	}

	config, err := pages_service.GetPagesConfig(ctx, repo)
	if err != nil || !config.Enabled {
		ctx.APIErrorNotFound("Pages not enabled")
		return
	}

	// Load README content
	readme := loadReadmeContent(ctx, repo)

	// Build title
	title := config.SEO.Title
	if title == "" {
		title = config.Hero.Title
	}
	if title == "" {
		title = repo.Name
	}

	// Build description
	description := config.SEO.Description
	if description == "" {
		description = config.Hero.Tagline
	}
	if description == "" {
		description = repo.Description
	}

	response := &PagesContentResponse{
		Title:       title,
		Description: description,
		Readme:      readme,
	}

	ctx.JSON(http.StatusOK, response)
}

// loadReadmeContent loads the README content from the repository
func loadReadmeContent(ctx *context.APIContext, repo *repo_model.Repository) string {
	gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
	if err != nil {
		return ""
	}
	defer gitRepo.Close()

	branch := repo.DefaultBranch
	if branch == "" {
		branch = "main"
	}

	commit, err := gitRepo.GetBranchCommit(branch)
	if err != nil {
		return ""
	}

	// Try common README paths
	readmePaths := []string{
		"README.md",
		"readme.md",
		"Readme.md",
		"README.markdown",
		"README.txt",
		"README",
	}

	for _, path := range readmePaths {
		entry, err := commit.GetTreeEntryByPath(path)
		if err != nil {
			continue
		}

		reader, err := entry.Blob().DataAsync()
		if err != nil {
			continue
		}

		content := make([]byte, entry.Blob().Size())
		_, err = reader.Read(content)
		reader.Close()

		if err != nil && err.Error() != "EOF" {
			continue
		}

		return string(content)
	}

	return ""
}
