package service

import (
	"context"

	complaint "wucher/internal/domain/complaint"
	"wucher/internal/shared/pkg/userctx"
)

type ComplaintService struct{ repo complaint.Repository }

func NewComplaintService(repo complaint.Repository) *ComplaintService {
	return &ComplaintService{repo: repo}
}

func (s *ComplaintService) Create(ctx context.Context, row *complaint.Complaint) error {
	return s.repo.Create(ctx, row)
}
func (s *ComplaintService) Update(ctx context.Context, row *complaint.Complaint) error {
	return s.repo.Update(ctx, row)
}
func (s *ComplaintService) Delete(ctx context.Context, id []byte, deletedBy []byte) error {
	return s.repo.Delete(ctx, id, deletedBy)
}
func (s *ComplaintService) GetByID(ctx context.Context, id []byte) (*complaint.Complaint, error) {
	return s.repo.GetByID(ctx, id)
}
func (s *ComplaintService) ListByHelicopter(ctx context.Context, helicopterID []byte, limit, offset int) ([]complaint.Complaint, int64, error) {
	return s.repo.ListByHelicopter(ctx, helicopterID, limit, offset)
}
func (s *ComplaintService) List(ctx context.Context, filter complaint.ListFilter, sort string, limit, offset int) ([]complaint.Complaint, int64, error) {
	return s.repo.List(ctx, filter, sort, limit, offset)
}
func (s *ComplaintService) HelicopterHasOpenComplaint(ctx context.Context, helicopterID []byte) (bool, error) {
	return s.repo.HelicopterHasOpenComplaint(ctx, helicopterID)
}
func (s *ComplaintService) ActiveGroundingByHelicopterIDs(ctx context.Context, helicopterIDs [][]byte) ([]complaint.Complaint, error) {
	return s.repo.ActiveGroundingByHelicopterIDs(ctx, helicopterIDs)
}
func (s *ComplaintService) OpenByHelicopterIDs(ctx context.Context, helicopterIDs [][]byte) ([]complaint.Complaint, error) {
	return s.repo.OpenByHelicopterIDs(ctx, helicopterIDs)
}
func (s *ComplaintService) HelicopterHasUnactionedComplaint(ctx context.Context, helicopterID []byte) (bool, error) {
	return s.repo.HelicopterHasUnactionedComplaint(ctx, helicopterID)
}
func (s *ComplaintService) MarkFixedByHelicopter(ctx context.Context, helicopterID []byte, easaID []byte, actor []byte) error {
	return s.repo.MarkFixedByHelicopter(ctx, helicopterID, easaID, actor)
}
func (s *ComplaintService) MarkFixedByComplaint(ctx context.Context, complaintID []byte, easaID []byte, actor []byte) error {
	return s.repo.MarkFixedByComplaint(ctx, complaintID, easaID, actor)
}
func (s *ComplaintService) MarkFixedByFlight(ctx context.Context, flightID []byte, easaID []byte, actor []byte) error {
	return s.repo.MarkFixedByFlight(ctx, flightID, easaID, actor)
}
func (s *ComplaintService) ResolveUserNames(ctx context.Context, complaints []complaint.Complaint) (map[string]string, error) {
	out := make(map[string]string)
	add := func(id []byte) {
		if len(id) == 0 {
			return
		}
		if _, ok := out[string(id)]; ok {
			return
		}
		if n := userctx.GetDisplayName(ctx, id); n != "" {
			out[string(id)] = n
		}
	}
	for i := range complaints {
		add(complaints[i].ReportedBy)
		add(complaints[i].MELClassifiedBy)
		add(complaints[i].NSRDecidedBy)
	}
	return out, nil
}
