package dispute

import "errors"

var (
	ErrDependencyUnavailable = errors.New("dependency unavailable")
	ErrPermissionDenied      = errors.New("permission denied")
	ErrInvalidDispute        = errors.New("invalid dispute")
	ErrDisputeExists         = errors.New("dispute already exists")
	ErrDisputeCannotHandle   = errors.New("dispute cannot handle")
)

type Service struct {
	repo *Repository
}

func NewService(repo *Repository) *Service {
	return &Service{repo: repo}
}

func (s *Service) Create(userID uint64, orderID uint64, req CreateRequest) (*DisputeDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if orderID == 0 || req.Type == "" || req.Description == "" {
		return nil, ErrInvalidDispute
	}
	return s.repo.Create(userID, orderID, req)
}

func (s *Service) ListForUser(userID uint64, page, pageSize int) (*PaginatedResult, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.ListForUser(userID, page, pageSize)
}

func (s *Service) FindForUser(userID uint64, id uint64) (*DisputeDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.FindForUser(userID, id)
}

func (s *Service) ListAdmin(page, pageSize int) (*PaginatedResult, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.ListAdmin(page, pageSize)
}

func (s *Service) Arbitrate(adminID uint64, id uint64, req ArbitrateRequest, meta AuditMeta) (*DisputeDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if id == 0 || req.Result == "" || req.Remark == "" {
		return nil, ErrInvalidDispute
	}
	return s.repo.Arbitrate(adminID, id, req, meta)
}
