// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package setting

import (
	"net/http"

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

const tplRepoSettingsPages templates.TplName = "repo/settings/pages"

// Pages shows the repository pages settings
func Pages(ctx *context.Context) {
	ctx.Data["Title"] = ctx.Tr("repo.settings.pages")
	ctx.Data["PageIsSettingsPages"] = true

	// Get pages config
	config, err := repo_model.GetPagesConfig(ctx, ctx.Repo.Repository.ID)
	if err != nil && !repo_model.IsErrPagesConfigNotExist(err) {
		ctx.ServerError("GetPagesConfig", err)
		return
	}

	if config != nil {
		ctx.Data["PagesEnabled"] = config.Enabled
		ctx.Data["PagesTemplate"] = config.Template
	}

	// Get pages domains
	domains, err := repo_model.GetPagesDomains(ctx, ctx.Repo.Repository.ID)
	if err != nil {
		ctx.ServerError("GetPagesDomains", err)
		return
	}
	ctx.Data["PagesDomains"] = domains

	// Generate subdomain
	ctx.Data["PagesSubdomain"] = pages_service.GetPagesSubdomain(ctx.Repo.Repository)

	// Available templates
	ctx.Data["PagesTemplates"] = []string{"simple", "documentation", "product", "portfolio"}

	ctx.HTML(http.StatusOK, tplRepoSettingsPages)
}

// PagesPost handles the pages settings form submission
func PagesPost(ctx *context.Context) {
	ctx.Data["Title"] = ctx.Tr("repo.settings.pages")
	ctx.Data["PageIsSettingsPages"] = true

	action := ctx.FormString("action")

	switch action {
	case "enable":
		template := ctx.FormString("template")
		if template == "" {
			template = "simple"
		}
		if err := pages_service.EnablePages(ctx, ctx.Repo.Repository, template); err != nil {
			ctx.ServerError("EnablePages", err)
			return
		}
		ctx.Flash.Success(ctx.Tr("repo.settings.pages.enabled_success"))

	case "disable":
		if err := pages_service.DisablePages(ctx, ctx.Repo.Repository); err != nil {
			ctx.ServerError("DisablePages", err)
			return
		}
		ctx.Flash.Success(ctx.Tr("repo.settings.pages.disabled_success"))

	case "update_template":
		template := ctx.FormString("template")
		if template == "" {
			template = "simple"
		}
		if err := pages_service.EnablePages(ctx, ctx.Repo.Repository, template); err != nil {
			ctx.ServerError("EnablePages", err)
			return
		}
		ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))

	case "add_domain":
		domain := ctx.FormString("domain")
		if domain == "" {
			ctx.Flash.Error(ctx.Tr("repo.settings.pages.domain_required"))
			ctx.Redirect(ctx.Repo.Repository.Link() + "/settings/pages")
			return
		}
		_, err := pages_service.AddPagesDomain(ctx, ctx.Repo.Repository.ID, domain)
		if err != nil {
			if repo_model.IsErrPagesDomainAlreadyExist(err) {
				ctx.Flash.Error(ctx.Tr("repo.settings.pages.domain_exists"))
			} else {
				ctx.ServerError("AddPagesDomain", err)
				return
			}
		} else {
			ctx.Flash.Success(ctx.Tr("repo.settings.pages.domain_added"))
		}

	case "delete_domain":
		domainID := ctx.FormInt64("domain_id")
		if err := repo_model.DeletePagesDomain(ctx, domainID); err != nil {
			ctx.ServerError("DeletePagesDomain", err)
			return
		}
		ctx.Flash.Success(ctx.Tr("repo.settings.pages.domain_deleted"))

	case "verify_domain":
		domainID := ctx.FormInt64("domain_id")
		if err := pages_service.VerifyDomain(ctx, domainID); err != nil {
			if err.Error() == "DNS verification failed" {
				ctx.Flash.Error(ctx.Tr("repo.settings.pages.domain_verification_failed"))
			} else {
				ctx.ServerError("VerifyDomain", err)
				return
			}
		} else {
			ctx.Flash.Success(ctx.Tr("repo.settings.pages.domain_verified"))
		}

	default:
		ctx.NotFound(nil)
		return
	}

	ctx.Redirect(ctx.Repo.Repository.Link() + "/settings/pages")
}
