package main

import (
	"src.opensuse.org/autogits/common"
	"src.opensuse.org/autogits/common/gitea-generated/models"
)

type PRReviews struct {
	reviews   []*models.PullReview
	reviewers []string
}

func FetchGiteaReviews(rf common.GiteaReviewFetcher, reviewers []string, org, repo string, no int64) (*PRReviews, error) {
	reviews, err := rf.GetPullRequestReviews(org, repo, no)
	if err != nil {
		return nil, err
	}

	return &PRReviews{
		reviews:   reviews,
		reviewers: reviewers,
	}, nil
}

func (r *PRReviews) IsReviewed() bool {
	goodReview := false

	for _, reviewer := range r.reviewers {
		goodReview = false
		for _, review := range r.reviews {
			if review.User.UserName == reviewer && review.State == common.ReviewStateApproved && !review.Stale {
				goodReview = true
				break
			}
		}

		if !goodReview {
			break
		}
	}

	return goodReview
}
