package product

import (
	"context"
	"fmt"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)

type Repository interface {
	GetProducts(ctx context.Context, page, pageSize uint) ([]Product, error)
}

type Service struct {
	repository Repository
}

func New(repo Repository) Service {
	return Service{
		repository: repo,
	}
}

func (s Service) GetProducts(ctx context.Context, req GetProductListRequest) (GetProductListResponse, error) {

	page := req.GetPageNumber()
	pageSize := req.PageSize

	products, err := s.repository.GetProducts(ctx, page, pageSize)
	if err != nil {
		fmt.Println(err)
		return GetProductListResponse{}, errmsg.ErrorResponse{
			Message: ErrFailedGetProductList.Error(),
			Errors:  map[string]interface{}{"repository_error": err.Error()},
		}
	}

	return GetProductListResponse{Data: products}, nil
}
