// Copyright 2026 Moko Consulting <hello@mokoconsulting.tech>
// SPDX-License-Identifier: GPL-3.0-or-later

package setting

import (
	"net/http"
	"strconv"

	issues_model "code.mokoconsulting.tech/MokoConsulting/MokoGitea/models/issues"
	"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/templates"
	"code.mokoconsulting.tech/MokoConsulting/MokoGitea/services/context"
)

const tplSettingsCustomFields templates.TplName = "repo/settings/custom_fields"

// CustomFields displays the custom fields settings page.
func CustomFields(ctx *context.Context) {
	ctx.Data["Title"] = ctx.Tr("repo.settings.custom_fields")
	ctx.Data["PageIsSettingsCustomFields"] = true

	fields, err := issues_model.GetAllCustomFieldsByRepo(ctx, ctx.Repo.Repository.ID)
	if err != nil {
		ctx.ServerError("GetAllCustomFieldsByRepo", err)
		return
	}
	ctx.Data["CustomFields"] = fields

	ctx.HTML(http.StatusOK, tplSettingsCustomFields)
}

// CustomFieldsCreatePost creates a new custom field definition.
func CustomFieldsCreatePost(ctx *context.Context) {
	sortOrder, _ := strconv.Atoi(ctx.FormString("sort_order"))

	field := &issues_model.CustomFieldDef{
		RepoID:      ctx.Repo.Repository.ID,
		Name:        ctx.FormString("name"),
		FieldType:   issues_model.CustomFieldType(ctx.FormString("field_type")),
		Description: ctx.FormString("description"),
		Options:     ctx.FormString("options"),
		Required:    ctx.FormString("required") == "on",
		SortOrder:   sortOrder,
		IsActive:    true,
	}

	if field.Name == "" {
		ctx.Flash.Error("Field name is required")
		ctx.Redirect(ctx.Repo.RepoLink + "/settings/custom-fields")
		return
	}

	if err := issues_model.CreateCustomFieldDef(ctx, field); err != nil {
		ctx.ServerError("CreateCustomFieldDef", err)
		return
	}

	ctx.Flash.Success(ctx.Tr("repo.settings.custom_field_created"))
	ctx.Redirect(ctx.Repo.RepoLink + "/settings/custom-fields")
}

// CustomFieldsEditPost updates a custom field definition.
func CustomFieldsEditPost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	field, err := issues_model.GetCustomFieldDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetCustomFieldDefByID", err)
		return
	}

	if field.RepoID != ctx.Repo.Repository.ID {
		ctx.NotFound(nil)
		return
	}

	field.Name = ctx.FormString("name")
	field.FieldType = issues_model.CustomFieldType(ctx.FormString("field_type"))
	field.Description = ctx.FormString("description")
	field.Options = ctx.FormString("options")
	field.Required = ctx.FormString("required") == "on"
	field.IsActive = ctx.FormString("is_active") == "on"
	sortOrder, _ := strconv.Atoi(ctx.FormString("sort_order"))
	field.SortOrder = sortOrder

	if err := issues_model.UpdateCustomFieldDef(ctx, field); err != nil {
		ctx.ServerError("UpdateCustomFieldDef", err)
		return
	}

	ctx.Flash.Success(ctx.Tr("repo.settings.custom_field_updated"))
	ctx.Redirect(ctx.Repo.RepoLink + "/settings/custom-fields")
}

// CustomFieldsDeletePost deletes a custom field definition.
func CustomFieldsDeletePost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	field, err := issues_model.GetCustomFieldDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetCustomFieldDefByID", err)
		return
	}
	if field.RepoID != ctx.Repo.Repository.ID {
		ctx.NotFound(nil)
		return
	}

	if err := issues_model.DeleteCustomFieldDef(ctx, id); err != nil {
		ctx.ServerError("DeleteCustomFieldDef", err)
		return
	}

	ctx.Flash.Success(ctx.Tr("repo.settings.custom_field_deleted"))
	ctx.Redirect(ctx.Repo.RepoLink + "/settings/custom-fields")
}
