package service

import (
	"context"

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

type HospitalService struct {
	repo hospital.Repository
}

func NewHospitalService(repo hospital.Repository) *HospitalService {
	return &HospitalService{repo: repo}
}

func (s *HospitalService) Create(ctx context.Context, row *hospital.Hospital) error {
	return s.repo.Create(ctx, row)
}

func (s *HospitalService) Update(ctx context.Context, row *hospital.Hospital) error {
	return s.repo.Update(ctx, row)
}

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

func (s *HospitalService) GetByID(ctx context.Context, id []byte) (*hospital.Hospital, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *HospitalService) List(ctx context.Context, filter, sort string, limit, offset int) ([]hospital.Hospital, int64, error) {
	return s.repo.List(ctx, filter, normalizeHospitalSort(sort), limit, offset)
}

func normalizeHospitalSort(sort string) string {
	return normalizeSortByRules(sort,
		sortExpr(
			joinSortClauses(sortkey.ActivePositiveSortClauses("", "is_active", "sortkey", "hospital_name", false)),
			joinSortClauses(sortkey.ActivePositiveSortClauses("", "is_active", "sortkey", "hospital_name", true)),
			"sortkey",
		),
		sortField("hospital_name", "name", "hospital_name"),
		sortField("address"),
		sortField("landline_number"),
		sortField("mobile_number"),
		sortField("email"),
		sortField("note"),
		sortField("created_at"),
		sortField("updated_at"),
		sortField("is_active"),
	)
}
