package service

import (
	"context"

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

type ICAOService struct {
	repo icao.Repository
}

func NewICAOService(repo icao.Repository) *ICAOService {
	return &ICAOService{repo: repo}
}

func (s *ICAOService) Create(ctx context.Context, row *icao.ICAO) error {
	return s.repo.Create(ctx, row)
}

func (s *ICAOService) Update(ctx context.Context, row *icao.ICAO) error {
	return s.repo.Update(ctx, row)
}

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

func (s *ICAOService) GetByID(ctx context.Context, id []byte) (*icao.ICAO, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *ICAOService) List(ctx context.Context, filter, sort string, limit, offset int) ([]icao.ICAO, int64, error) {
	return s.repo.List(ctx, filter, normalizeICAOSort(sort), limit, offset)
}

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