package service

import (
	"context"

	"wucher/internal/domain/vocation"
	"wucher/internal/shared/pkg/sortkey"
)

type VocationService struct {
	repo vocation.Repository
}

func NewVocationService(repo vocation.Repository) *VocationService {
	return &VocationService{repo: repo}
}

func (s *VocationService) Create(ctx context.Context, v *vocation.Vocation) error {
	return s.repo.Create(ctx, v)
}

func (s *VocationService) Update(ctx context.Context, v *vocation.Vocation) error {
	return s.repo.Update(ctx, v)
}

func (s *VocationService) Delete(ctx context.Context, id []byte, deletedBy []byte) error {
	return s.repo.Delete(ctx, id, deletedBy)
}

func (s *VocationService) GetByID(ctx context.Context, id []byte) (*vocation.Vocation, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *VocationService) List(ctx context.Context, filter, sort string, limit, offset int) ([]vocation.Vocation, int64, error) {
	return s.repo.List(ctx, filter, normalizeVocationSort(sort), limit, offset)
}

func normalizeVocationSort(sort string) string {
	return normalizeSortByRules(sort,
		sortExpr(
			joinSortClauses(sortkey.ActivePositiveSortClauses("", "is_active", "sortkey", "name", false)),
			joinSortClauses(sortkey.ActivePositiveSortClauses("", "is_active", "sortkey", "name", true)),
			"sortkey",
		),
		sortField("name"),
		sortField("note"),
		sortField("is_active"),
		sortField("created_at"),
		sortField("updated_at"),
	)
}
