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

// SettingsIssuePriorities shows the org-level issue priorities management page.
func SettingsIssuePriorities(ctx *context.Context) {
	ctx.Data["Title"] = ctx.Tr("org.settings.issue_priorities")
	ctx.Data["PageIsOrgSettings"] = true
	ctx.Data["PageIsSettingsIssuePriorities"] = true

	defs, err := issues_model.GetAllIssuePriorityDefsByOrg(ctx, ctx.Org.Organization.ID)
	if err != nil {
		ctx.ServerError("GetAllIssuePriorityDefsByOrg", err)
		return
	}
	ctx.Data["IssuePriorities"] = defs

	ctx.HTML(http.StatusOK, tplOrgIssuePriorities)
}

// SettingsIssuePrioritiesCreatePost creates a new org-level issue priority.
func SettingsIssuePrioritiesCreatePost(ctx *context.Context) {
	sortOrder, _ := strconv.Atoi(ctx.FormString("sort_order"))

	def := &issues_model.IssuePriorityDef{
		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("Priority name is required")
		ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-priorities")
		return
	}

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

	ctx.Flash.Success(ctx.Tr("org.settings.issue_priority_created"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-priorities")
}

// SettingsIssuePrioritiesEditPost updates an org-level issue priority.
func SettingsIssuePrioritiesEditPost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	def, err := issues_model.GetIssuePriorityDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetIssuePriorityDefByID", 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.UpdateIssuePriorityDef(ctx, def); err != nil {
		ctx.ServerError("UpdateIssuePriorityDef", err)
		return
	}

	ctx.Flash.Success(ctx.Tr("org.settings.issue_priority_updated"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-priorities")
}

// SettingsIssuePrioritiesDeletePost deletes an org-level issue priority.
func SettingsIssuePrioritiesDeletePost(ctx *context.Context) {
	id := ctx.PathParamInt64("id")
	def, err := issues_model.GetIssuePriorityDefByID(ctx, id)
	if err != nil {
		ctx.ServerError("GetIssuePriorityDefByID", err)
		return
	}
	if def.OrgID != ctx.Org.Organization.ID {
		ctx.NotFound(nil)
		return
	}

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

	ctx.Flash.Success(ctx.Tr("org.settings.issue_priority_deleted"))
	ctx.Redirect(ctx.Org.OrgLink + "/settings/issue-priorities")
}
