package service

import (
	"context"

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

type ForcesPresentService struct {
	repo forces_present.Repository
}

func NewForcesPresentService(repo forces_present.Repository) *ForcesPresentService {
	return &ForcesPresentService{repo: repo}
}

func (s *ForcesPresentService) Create(ctx context.Context, row *forces_present.ForcesPresent) error {
	return s.repo.Create(ctx, row)
}

func (s *ForcesPresentService) Update(ctx context.Context, row *forces_present.ForcesPresent) error {
	return s.repo.Update(ctx, row)
}

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

func (s *ForcesPresentService) GetByID(ctx context.Context, id []byte) (*forces_present.ForcesPresent, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *ForcesPresentService) List(ctx context.Context, filter, sort string, limit, offset int) ([]forces_present.ForcesPresent, int64, error) {
	return s.repo.List(ctx, filter, normalizeForcesPresentSort(sort), limit, offset)
}

func normalizeForcesPresentSort(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"),
	)
}
