package handler

import (
	"strconv"

	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"

	"gitee.ltd/lxh/wechat-robot/internal/model"
)

// ListContacts 列出机器人的联系人
func ListContacts(c *fiber.Ctx) error {
	robotID, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "无效的机器人ID")
	}

	var robot model.Robot
	db := model.GetDB()

	if err := db.First(&robot, robotID).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
		}
		return fiber.NewError(fiber.StatusInternalServerError, "查询数据库失败")
	}

	// 获取联系人列表
	var contacts []model.Contact
	if err = db.Where("robot_id = ?", robotID).Find(&contacts).Error; err != nil {
		return fiber.NewError(fiber.StatusInternalServerError, "获取联系人列表失败")
	}

	return c.Render("contact/index", fiber.Map{
		"Title":    "联系人列表",
		"Robot":    robot,
		"Contacts": contacts,
	})
}

// ShowContact 显示联系人详情与聊天记录
func ShowContact(c *fiber.Ctx) error {
	robotID, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "无效的机器人ID")
	}

	contactID, err := strconv.Atoi(c.Params("contactId"))
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "无效的联系人ID")
	}

	db := model.GetDB()

	// 获取机器人信息
	var robot model.Robot
	if err := db.First(&robot, robotID).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return fiber.NewError(fiber.StatusNotFound, "机器人不存在")
		}
		return fiber.NewError(fiber.StatusInternalServerError, "查询机器人失败")
	}

	// 获取联系人信息
	var contact model.Contact
	if err := db.First(&contact, contactID).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return fiber.NewError(fiber.StatusNotFound, "联系人不存在")
		}
		return fiber.NewError(fiber.StatusInternalServerError, "查询联系人失败")
	}

	// 获取聊天记录
	var messages []model.Message
	if err := db.Where("robot_id = ? AND contact_id = ?", robotID, contactID).
		Order("send_time DESC").Limit(50).Find(&messages).Error; err != nil {
		return fiber.NewError(fiber.StatusInternalServerError, "获取聊天记录失败")
	}

	return c.Render("contact/show", fiber.Map{
		"Title":    contact.Nickname,
		"Robot":    robot,
		"Contact":  contact,
		"Messages": messages,
	})
}

// ListGroupMembers 列出群成员
func ListGroupMembers(c *fiber.Ctx) error {
	robotID, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "无效的机器人ID")
	}

	contactID, err := strconv.Atoi(c.Params("contactId"))
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, "无效的联系人ID")
	}

	db := model.GetDB()

	// 获取机器人信息
	var robot model.Robot
	if err := db.First(&robot, robotID).Error; err != nil {
		return fiber.NewError(fiber.StatusInternalServerError, "查询机器人失败")
	}

	// 获取联系人信息（群）
	var contact model.Contact
	if err := db.First(&contact, contactID).Error; err != nil {
		return fiber.NewError(fiber.StatusInternalServerError, "查询群信息失败")
	}

	// 确保联系人是群组
	if contact.Type != model.ContactTypeGroup {
		return fiber.NewError(fiber.StatusBadRequest, "指定的联系人不是群组")
	}

	// 获取群成员
	var members []model.GroupMember
	if err = db.Where("group_id = ?", contactID).Find(&members).Error; err != nil {
		return fiber.NewError(fiber.StatusInternalServerError, "获取群成员失败")
	}

	return c.Render("contact/members", fiber.Map{
		"Title":   contact.Nickname + " 群成员",
		"Robot":   robot,
		"Contact": contact,
		"Members": members,
	})
}
