package collection

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

// CollectionCommands contains the command handlers for the collection aggregate.
type CollectionCommands struct {
	repo domain.CollectionRepository
}

// NewCollectionCommands creates a new CollectionCommands handler.
func NewCollectionCommands(repo domain.CollectionRepository) *CollectionCommands {
	return &CollectionCommands{repo: repo}
}

// CreateCollectionInput represents the input for creating a new collection.
type CreateCollectionInput struct {
	Name          string
	Description   string
	UserID        uint
	IsPublic      bool
	CoverImageURL string
}

// CreateCollection creates a new collection.
func (c *CollectionCommands) CreateCollection(ctx context.Context, input CreateCollectionInput) (*domain.Collection, error) {
	collection := &domain.Collection{
		Name:          input.Name,
		Description:   input.Description,
		UserID:        input.UserID,
		IsPublic:      input.IsPublic,
		CoverImageURL: input.CoverImageURL,
	}
	err := c.repo.Create(ctx, collection)
	if err != nil {
		return nil, err
	}
	return collection, nil
}

// UpdateCollectionInput represents the input for updating an existing collection.
type UpdateCollectionInput struct {
	ID            uint
	Name          string
	Description   string
	IsPublic      bool
	CoverImageURL string
	UserID        uint
}

// UpdateCollection updates an existing collection.
func (c *CollectionCommands) UpdateCollection(ctx context.Context, input UpdateCollectionInput) (*domain.Collection, error) {
	collection, err := c.repo.GetByID(ctx, input.ID)
	if err != nil {
		return nil, err
	}
	if collection.UserID != input.UserID {
		return nil, fmt.Errorf("unauthorized: user %d cannot update collection %d", input.UserID, input.ID)
	}
	collection.Name = input.Name
	collection.Description = input.Description
	collection.IsPublic = input.IsPublic
	collection.CoverImageURL = input.CoverImageURL
	err = c.repo.Update(ctx, collection)
	if err != nil {
		return nil, err
	}
	return collection, nil
}

// DeleteCollection deletes a collection by ID.
func (c *CollectionCommands) DeleteCollection(ctx context.Context, id uint, userID uint) error {
	collection, err := c.repo.GetByID(ctx, id)
	if err != nil {
		return err
	}
	if collection.UserID != userID {
		return fmt.Errorf("unauthorized: user %d cannot delete collection %d", userID, id)
	}
	return c.repo.Delete(ctx, id)
}

// AddWorkToCollectionInput represents the input for adding a work to a collection.
type AddWorkToCollectionInput struct {
	CollectionID uint
	WorkID       uint
	UserID       uint
}

// AddWorkToCollection adds a work to a collection.
func (c *CollectionCommands) AddWorkToCollection(ctx context.Context, input AddWorkToCollectionInput) error {
	collection, err := c.repo.GetByID(ctx, input.CollectionID)
	if err != nil {
		return err
	}
	if collection.UserID != input.UserID {
		return fmt.Errorf("unauthorized: user %d cannot add work to collection %d", input.UserID, input.CollectionID)
	}
	return c.repo.AddWorkToCollection(ctx, input.CollectionID, input.WorkID)
}

// RemoveWorkFromCollectionInput represents the input for removing a work from a collection.
type RemoveWorkFromCollectionInput struct {
	CollectionID uint
	WorkID       uint
	UserID       uint
}

// RemoveWorkFromCollection removes a work from a collection.
func (c *CollectionCommands) RemoveWorkFromCollection(ctx context.Context, input RemoveWorkFromCollectionInput) error {
	collection, err := c.repo.GetByID(ctx, input.CollectionID)
	if err != nil {
		return err
	}
	if collection.UserID != input.UserID {
		return fmt.Errorf("unauthorized: user %d cannot remove work from collection %d", input.UserID, input.CollectionID)
	}
	return c.repo.RemoveWorkFromCollection(ctx, input.CollectionID, input.WorkID)
}
