package copyright

import (
	"context"
	"errors"
	"tercul/internal/domain"
	"tercul/internal/platform/log"
)

// CopyrightCommands contains the command handlers for copyright.
type CopyrightCommands struct {
	repo domain.CopyrightRepository
}

// NewCopyrightCommands creates a new CopyrightCommands handler.
func NewCopyrightCommands(repo domain.CopyrightRepository) *CopyrightCommands {
	return &CopyrightCommands{repo: repo}
}

// CreateCopyright creates a new copyright.
func (c *CopyrightCommands) CreateCopyright(ctx context.Context, copyright *domain.Copyright) error {
	if copyright == nil {
		return errors.New("copyright cannot be nil")
	}
	if copyright.Name == "" {
		return errors.New("copyright name cannot be empty")
	}
	if copyright.Identificator == "" {
		return errors.New("copyright identificator cannot be empty")
	}
	log.FromContext(ctx).With("name", copyright.Name).Debug("Creating copyright")
	return c.repo.Create(ctx, copyright)
}

// UpdateCopyright updates an existing copyright.
func (c *CopyrightCommands) UpdateCopyright(ctx context.Context, copyright *domain.Copyright) error {
	if copyright == nil {
		return errors.New("copyright cannot be nil")
	}
	if copyright.ID == 0 {
		return errors.New("copyright ID cannot be zero")
	}
	if copyright.Name == "" {
		return errors.New("copyright name cannot be empty")
	}
	if copyright.Identificator == "" {
		return errors.New("copyright identificator cannot be empty")
	}
	log.FromContext(ctx).With("id", copyright.ID).Debug("Updating copyright")
	return c.repo.Update(ctx, copyright)
}

// DeleteCopyright deletes a copyright.
func (c *CopyrightCommands) DeleteCopyright(ctx context.Context, id uint) error {
	if id == 0 {
		return errors.New("invalid copyright ID")
	}
	log.FromContext(ctx).With("id", id).Debug("Deleting copyright")
	return c.repo.Delete(ctx, id)
}

// AddCopyrightToWork adds a copyright to a work.
func (c *CopyrightCommands) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
	if workID == 0 || copyrightID == 0 {
		return errors.New("invalid work ID or copyright ID")
	}
	log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Adding copyright to work")
	return c.repo.AddCopyrightToWork(ctx, workID, copyrightID)
}

// RemoveCopyrightFromWork removes a copyright from a work.
func (c *CopyrightCommands) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
	if workID == 0 || copyrightID == 0 {
		return errors.New("invalid work ID or copyright ID")
	}
	log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Removing copyright from work")
	return c.repo.RemoveCopyrightFromWork(ctx, workID, copyrightID)
}

// AddCopyrightToAuthor adds a copyright to an author.
func (c *CopyrightCommands) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
	if authorID == 0 || copyrightID == 0 {
		return errors.New("invalid author ID or copyright ID")
	}
	log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Adding copyright to author")
	return c.repo.AddCopyrightToAuthor(ctx, authorID, copyrightID)
}

// RemoveCopyrightFromAuthor removes a copyright from an author.
func (c *CopyrightCommands) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
	if authorID == 0 || copyrightID == 0 {
		return errors.New("invalid author ID or copyright ID")
	}
	log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Removing copyright from author")
	return c.repo.RemoveCopyrightFromAuthor(ctx, authorID, copyrightID)
}

// AddCopyrightToBook adds a copyright to a book.
func (c *CopyrightCommands) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
	if bookID == 0 || copyrightID == 0 {
		return errors.New("invalid book ID or copyright ID")
	}
	log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Adding copyright to book")
	return c.repo.AddCopyrightToBook(ctx, bookID, copyrightID)
}

// RemoveCopyrightFromBook removes a copyright from a book.
func (c *CopyrightCommands) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
	if bookID == 0 || copyrightID == 0 {
		return errors.New("invalid book ID or copyright ID")
	}
	log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Removing copyright from book")
	return c.repo.RemoveCopyrightFromBook(ctx, bookID, copyrightID)
}

// AddCopyrightToPublisher adds a copyright to a publisher.
func (c *CopyrightCommands) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
	if publisherID == 0 || copyrightID == 0 {
		return errors.New("invalid publisher ID or copyright ID")
	}
	log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Adding copyright to publisher")
	return c.repo.AddCopyrightToPublisher(ctx, publisherID, copyrightID)
}

// RemoveCopyrightFromPublisher removes a copyright from a publisher.
func (c *CopyrightCommands) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
	if publisherID == 0 || copyrightID == 0 {
		return errors.New("invalid publisher ID or copyright ID")
	}
	log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Removing copyright from publisher")
	return c.repo.RemoveCopyrightFromPublisher(ctx, publisherID, copyrightID)
}

// AddCopyrightToSource adds a copyright to a source.
func (c *CopyrightCommands) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
	if sourceID == 0 || copyrightID == 0 {
		return errors.New("invalid source ID or copyright ID")
	}
	log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Adding copyright to source")
	return c.repo.AddCopyrightToSource(ctx, sourceID, copyrightID)
}

// RemoveCopyrightFromSource removes a copyright from a source.
func (c *CopyrightCommands) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
	if sourceID == 0 || copyrightID == 0 {
		return errors.New("invalid source ID or copyright ID")
	}
	log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Removing copyright from source")
	return c.repo.RemoveCopyrightFromSource(ctx, sourceID, copyrightID)
}

// AddTranslation adds a translation to a copyright.
func (c *CopyrightCommands) AddTranslation(ctx context.Context, translation *domain.CopyrightTranslation) error {
	if translation == nil {
		return errors.New("translation cannot be nil")
	}
	if translation.CopyrightID == 0 {
		return errors.New("copyright ID cannot be zero")
	}
	if translation.LanguageCode == "" {
		return errors.New("language code cannot be empty")
	}
	if translation.Message == "" {
		return errors.New("translation message cannot be empty")
	}
	log.FromContext(ctx).With("copyright_id", translation.CopyrightID).With("language", translation.LanguageCode).Debug("Adding translation to copyright")
	return c.repo.AddTranslation(ctx, translation)
}
