package service

import (
	"awesome-back/internal/domain/entities"
	"awesome-back/internal/domain/repositories"
	"awesome-back/pkg/errors"
)

type PastryService struct {
	repo repositories.PastryRepository
}

func NewPastryService(repo repositories.PastryRepository) *PastryService {
	return &PastryService{repo: repo}
}

func (s *PastryService) GetAllPastries() ([]entities.Pastry, error) {
	pastries, err := s.repo.FindAll()
	if err != nil {
		return nil, errors.Wrap(err, "failed to get pastries")
	}
	return pastries, nil
}

func (s *PastryService) GetPastryByID(id int) (*entities.Pastry, error) {
	if id <= 0 {
		return nil, errors.NewInvalidError("invalid pastry ID")
	}

	pastry, err := s.repo.FindByID(id)
	if err != nil {
		if errors.IsNotFound(err) {
			return nil, errors.NewNotFoundError("pastry not found")
		}
		return nil, errors.Wrap(err, "failed to get pastry")
	}

	return pastry, nil
}

func (s *PastryService) GetPastriesByCategory(category string) ([]entities.Pastry, error) {
	if category == "" {
		return nil, errors.NewInvalidError("category cannot be empty")
	}

	pastries, err := s.repo.FindByCategory(category)
	if err != nil {
		return nil, errors.Wrap(err, "failed to get pastries by category")
	}

	return pastries, nil
}
