package tag

import (
	"context"
	"tercul/internal/domain"
)

// TagCommands contains the command handlers for the tag aggregate.
type TagCommands struct {
	repo domain.TagRepository
}

// NewTagCommands creates a new TagCommands handler.
func NewTagCommands(repo domain.TagRepository) *TagCommands {
	return &TagCommands{repo: repo}
}

// CreateTagInput represents the input for creating a new tag.
type CreateTagInput struct {
	Name        string
	Description string
}

// CreateTag creates a new tag.
func (c *TagCommands) CreateTag(ctx context.Context, input CreateTagInput) (*domain.Tag, error) {
	tag := &domain.Tag{
		Name:        input.Name,
		Description: input.Description,
	}
	err := c.repo.Create(ctx, tag)
	if err != nil {
		return nil, err
	}
	return tag, nil
}

// UpdateTagInput represents the input for updating an existing tag.
type UpdateTagInput struct {
	ID          uint
	Name        string
	Description string
}

// UpdateTag updates an existing tag.
func (c *TagCommands) UpdateTag(ctx context.Context, input UpdateTagInput) (*domain.Tag, error) {
	tag, err := c.repo.GetByID(ctx, input.ID)
	if err != nil {
		return nil, err
	}
	tag.Name = input.Name
	tag.Description = input.Description
	err = c.repo.Update(ctx, tag)
	if err != nil {
		return nil, err
	}
	return tag, nil
}

// DeleteTag deletes a tag by ID.
func (c *TagCommands) DeleteTag(ctx context.Context, id uint) error {
	return c.repo.Delete(ctx, id)
}
