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
}
