package common

import (
	"encoding/json"
	"fmt"
	"io"
)

type Head struct {
	Ref  string
	Repo *Repository
	Sha  string
}

type IssueLabelDetail struct {
	Id          int
	Name        string
	Exclusive   bool
	Is_archived bool
	Color       string
	Description string
	Url         string
}

type PullRequest struct {
	Id     int
	Url    string
	Number int
	State  string

	Base                Head
	Head                Head
	Labels              []IssueLabelDetail
	Requested_reviewers []*User

	User User
}

type PullRequestWebhookEvent struct {
	Action string
	Number int

	Pull_Request       *PullRequest
	Repository         *Repository
	Requested_reviewer *User
	Sender             User
}

func (p *PullRequestWebhookEvent) GetAction() string {
	return p.Action
}

func (h *RequestHandler) parsePullRequest(data io.Reader) (action *PullRequestWebhookEvent, err error) {
	action=new(PullRequestWebhookEvent)
	err = json.NewDecoder(data).Decode(&action)

	if err != nil {
		return nil, fmt.Errorf("Got error while parsing json: %w", err)
	}

	h.Request = &Request {
		Data: action,
		Type: RequestType_PR,
	}
	return
}

func (h *RequestHandler) parsePullRequestSync(data io.Reader) (*PullRequestWebhookEvent, error) {
	return h.parsePullRequest(data)
}