package repository

import (
	"x-agents/server/internal/model"

	"gorm.io/gorm"
)

type AgentRepository struct {
	db *gorm.DB
}

func NewAgentRepository(db *gorm.DB) *AgentRepository {
	return &AgentRepository{db: db}
}

func (r *AgentRepository) Create(agent *model.Agent) error {
	return r.db.Create(agent).Error
}

func (r *AgentRepository) FindByID(id string) (*model.Agent, error) {
	var agent model.Agent
	err := r.db.First(&agent, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &agent, nil
}

func (r *AgentRepository) FindByOwnerID(ownerID string) ([]model.Agent, error) {
	var agents []model.Agent
	err := r.db.Where("owner_id = ?", ownerID).Find(&agents).Error
	return agents, err
}

func (r *AgentRepository) FindAll() ([]model.Agent, error) {
	var agents []model.Agent
	err := r.db.Where("is_active = ?", true).Find(&agents).Error
	return agents, err
}

func (r *AgentRepository) Update(agent *model.Agent) error {
	return r.db.Save(agent).Error
}

func (r *AgentRepository) Delete(id string) error {
	return r.db.Delete(&model.Agent{}, "id = ?", id).Error
}

// AgentSkill 相关方法

func (r *AgentRepository) CreateAgentSkill(as *model.AgentSkill) error {
	return r.db.Create(as).Error
}

func (r *AgentRepository) FindSkillsByAgentID(agentID string) ([]model.AgentSkill, error) {
	var agentSkills []model.AgentSkill
	err := r.db.Where("agent_id = ?", agentID).Find(&agentSkills).Error
	return agentSkills, err
}

func (r *AgentRepository) DeleteAgentSkills(agentID string) error {
	return r.db.Delete(&model.AgentSkill{}, "agent_id = ?", agentID).Error
}

// AgentKnowledgeBase 相关方法

func (r *AgentRepository) CreateAgentKnowledgeBase(akb *model.AgentKnowledgeBase) error {
	return r.db.Create(akb).Error
}

func (r *AgentRepository) FindKnowledgeBasesByAgentID(agentID string) ([]model.AgentKnowledgeBase, error) {
	var agentKBs []model.AgentKnowledgeBase
	err := r.db.Where("agent_id = ?", agentID).Find(&agentKBs).Error
	return agentKBs, err
}

func (r *AgentRepository) DeleteAgentKnowledgeBases(agentID string) error {
	return r.db.Delete(&model.AgentKnowledgeBase{}, "agent_id = ?", agentID).Error
}

// AgentMemory 相关方法

func (r *AgentRepository) CreateMemory(memory *model.AgentMemory) error {
	return r.db.Create(memory).Error
}

func (r *AgentRepository) FindMemoriesByAgentID(agentID string, limit int) ([]model.AgentMemory, error) {
	var memories []model.AgentMemory
	err := r.db.Where("agent_id = ?", agentID).Order("importance DESC, created_at DESC").Limit(limit).Find(&memories).Error
	return memories, err
}

func (r *AgentRepository) FindMemoriesByUserID(agentID, userID string, limit int) ([]model.AgentMemory, error) {
	var memories []model.AgentMemory
	err := r.db.Where("agent_id = ? AND user_id = ?", agentID, userID).Order("importance DESC, created_at DESC").Limit(limit).Find(&memories).Error
	return memories, err
}

func (r *AgentRepository) DeleteMemory(id string) error {
	return r.db.Delete(&model.AgentMemory{}, "id = ?", id).Error
}

// AgentTeam 相关方法

func (r *AgentRepository) CreateAgentTeam(team *model.AgentTeam) error {
	return r.db.Create(team).Error
}

func (r *AgentRepository) FindTeamMembers(supervisorAgentID string) ([]model.AgentTeam, error) {
	var teams []model.AgentTeam
	err := r.db.Where("supervisor_agent_id = ?", supervisorAgentID).Find(&teams).Error
	return teams, err
}

func (r *AgentRepository) FindByMemberAgentID(memberAgentID string) ([]model.AgentTeam, error) {
	var teams []model.AgentTeam
	err := r.db.Where("member_agent_id = ?", memberAgentID).Find(&teams).Error
	return teams, err
}

func (r *AgentRepository) DeleteAgentTeam(id string) error {
	return r.db.Delete(&model.AgentTeam{}, "id = ?", id).Error
}

func (r *AgentRepository) DeleteTeamMembers(supervisorAgentID string) error {
	return r.db.Delete(&model.AgentTeam{}, "supervisor_agent_id = ?", supervisorAgentID).Error
}

// AgentTask 相关方法

func (r *AgentRepository) CreateTask(task *model.AgentTask) error {
	return r.db.Create(task).Error
}

func (r *AgentRepository) UpdateTask(task *model.AgentTask) error {
	return r.db.Save(task).Error
}

func (r *AgentRepository) FindTasksByAgentID(agentID string, limit int) ([]model.AgentTask, error) {
	var tasks []model.AgentTask
	err := r.db.Where("agent_id = ?", agentID).Order("created_at DESC").Limit(limit).Find(&tasks).Error
	return tasks, err
}

func (r *AgentRepository) FindTaskByID(id string) (*model.AgentTask, error) {
	var task model.AgentTask
	err := r.db.First(&task, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &task, nil
}
