package handler

import (
	"kra/internal/service"
	"kra/internal/service/dto"

	"github.com/gin-gonic/gin"
)

type Organization struct {
	departments *service.DepartmentService
	positions   *service.PositionService
}

func NewOrganization(departments *service.DepartmentService, positions *service.PositionService) *Organization {
	return &Organization{departments: departments, positions: positions}
}

func (h *Organization) ListDepartments(c *gin.Context) {
	var req dto.DepartmentListRequest
	_ = c.ShouldBindJSON(&req)
	items, err := h.departments.Departments(c.Request.Context(), req.Name)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, items, "获取成功")
}
func (h *Organization) CreateDepartment(c *gin.Context) {
	var req dto.DepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Name == "" {
		Fail(c, "部门名称不能为空")
		return
	}
	if err := h.departments.Create(c.Request.Context(), &req); err != nil {
		Fail(c, "创建失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "创建成功")
}
func (h *Organization) UpdateDepartment(c *gin.Context) {
	var req dto.DepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "部门ID不能为空")
		return
	}
	if err := h.departments.Update(c.Request.Context(), &req); err != nil {
		Fail(c, "更新失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "更新成功")
}
func (h *Organization) DeleteDepartment(c *gin.Context) {
	var req dto.DeleteDepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.departments.Delete(c.Request.Context(), req.ID); err != nil {
		Fail(c, "删除失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "删除成功")
}
func (h *Organization) FindDepartment(c *gin.Context) {
	var req dto.DeleteDepartmentRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	item, err := h.departments.Department(c.Request.Context(), req.ID)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, item, "获取成功")
}
func (h *Organization) DepartmentUsers(c *gin.Context) {
	var req dto.SetDepartmentUsersRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	ids, err := h.departments.UserIDs(c.Request.Context(), req.DepartmentID)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	if ids == nil {
		ids = []uint{}
	}
	Write(c, CodeSuccess, ids, "获取成功")
}
func (h *Organization) SetDepartmentUsers(c *gin.Context) {
	var req dto.SetDepartmentUsersRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.DepartmentID == 0 {
		Fail(c, "部门ID不能为空")
		return
	}
	if err := h.departments.SetUsers(c.Request.Context(), &req); err != nil {
		Fail(c, "设置失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}
func (h *Organization) SetUserDepartments(c *gin.Context) {
	var req dto.SetUserDepartmentsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "用户ID不能为空")
		return
	}
	if err := h.departments.SetUserDepartments(c.Request.Context(), &req); err != nil {
		Fail(c, "设置失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}

func (h *Organization) ListPositions(c *gin.Context) {
	var req dto.PositionListRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	items, total, err := h.positions.Positions(c.Request.Context(), &req)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
}
func (h *Organization) CreatePosition(c *gin.Context) {
	var req dto.PositionRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.Name == "" {
		Fail(c, "岗位名称不能为空")
		return
	}
	if err := h.positions.Create(c.Request.Context(), &req); err != nil {
		Fail(c, "创建失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "创建成功")
}
func (h *Organization) UpdatePosition(c *gin.Context) {
	var req dto.PositionRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "岗位ID不能为空")
		return
	}
	if err := h.positions.Update(c.Request.Context(), &req); err != nil {
		Fail(c, "更新失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "更新成功")
}
func (h *Organization) DeletePosition(c *gin.Context) {
	var req dto.DeletePositionRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.positions.Delete(c.Request.Context(), req.ID); err != nil {
		Fail(c, "删除失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "删除成功")
}
func (h *Organization) FindPosition(c *gin.Context) {
	var req dto.DeletePositionRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	item, err := h.positions.Position(c.Request.Context(), req.ID)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, item, "获取成功")
}
func (h *Organization) PositionUsers(c *gin.Context) {
	var req dto.SetPositionUsersRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	ids, err := h.positions.UserIDs(c.Request.Context(), req.PositionID)
	if err != nil {
		Fail(c, "获取失败:"+err.Error())
		return
	}
	if ids == nil {
		ids = []uint{}
	}
	Write(c, CodeSuccess, ids, "获取成功")
}
func (h *Organization) SetPositionUsers(c *gin.Context) {
	var req dto.SetPositionUsersRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.PositionID == 0 {
		Fail(c, "岗位ID不能为空")
		return
	}
	if err := h.positions.SetUsers(c.Request.Context(), &req); err != nil {
		Fail(c, "设置失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}
func (h *Organization) SetUserPositions(c *gin.Context) {
	var req dto.SetUserPositionsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if req.ID == 0 {
		Fail(c, "用户ID不能为空")
		return
	}
	if err := h.positions.SetUserPositions(c.Request.Context(), &req); err != nil {
		Fail(c, "设置失败:"+err.Error())
		return
	}
	Write(c, CodeSuccess, gin.H{}, "设置成功")
}
