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

package org

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 tplOrgIssueTypes templates.TplName = "org/settings/issue_types"

func SettingsIssueTypes(ctx *context.Context) {
	ctx.Data["Title"] = ctx.Tr("org.settings.issue_types")
	ctx.Data["PageIsOrgSettings"] = true
	ctx.Data["PageIsSettingsIssueTypes"] = true

	defs, err := issues_model.GetAllIssueTypeDefsByOrg(ctx, ctx.Org.Organization.ID)
	if err != nil {
		ctx.ServerError("GetAllIssueTypeDefsByOrg", err)
		return
	}
	ctx.Data["IssueTypes"] = defs
	ctx.HTML(http.StatusOK, tplOrgIssueTypes)
}

func SettingsIssueTypesCreatePost(ctx *context.Context) {
	sortOrder, _ := strconv.Atoi(ctx.FormString("sort_order"))
	def := &issues_model.IssueTypeDef{
		OrgID:       ctx.Org.Organization.ID,
		Name:        ctx.FormString("name"),
		Color:       ctx.FormString("color"),
		Description: ctx.FormString("description"),
		SortOrder:   sortOrder,
		IsDefault:   ctx.FormString("is_default") == "on",
		IsActive:    true,
	}
	if def.Name == "" {
		ctx.Flash.Error("Type name is required")
		ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-types")
		return
	}
	if err := issues_model.CreateIssueTypeDef(ctx, def); err != nil {
		ctx.ServerError("CreateIssueTypeDef", err)
		return
	}
	ctx.Flash.Success(ctx.Tr("org.settings.issue_type_created"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-types")
}

func SettingsIssueTypesEditPost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	def, err := issues_model.GetIssueTypeDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetIssueTypeDefByID", err)
		return
	}
	if def.OrgID != ctx.Org.Organization.ID {
		ctx.NotFound(nil)
		return
	}
	def.Name = ctx.FormString("name")
	def.Color = ctx.FormString("color")
	def.Description = ctx.FormString("description")
	def.IsDefault = ctx.FormString("is_default") == "on"
	def.IsActive = ctx.FormString("is_active") == "on"
	sortOrder, _ := strconv.Atoi(ctx.FormString("sort_order"))
	def.SortOrder = sortOrder
	if err := issues_model.UpdateIssueTypeDef(ctx, def); err != nil {
		ctx.ServerError("UpdateIssueTypeDef", err)
		return
	}
	ctx.Flash.Success(ctx.Tr("org.settings.issue_type_updated"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-types")
}

func SettingsIssueTypesDeletePost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	def, err := issues_model.GetIssueTypeDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetIssueTypeDefByID", err)
		return
	}
	if def.OrgID != ctx.Org.Organization.ID {
		ctx.NotFound(nil)
		return
	}
	if err := issues_model.DeleteIssueTypeDef(ctx, id); err != nil {
		ctx.ServerError("DeleteIssueTypeDef", err)
		return
	}
	ctx.Flash.Success(ctx.Tr("org.settings.issue_type_deleted"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-types")
}
