package gitea

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"log/slog"

	"code.gitea.io/sdk/gitea"
	"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
)

type Adapter struct {
	client *gitea.Client
}

func New(baseURL, token string) (*Adapter, error) {
	client, err := gitea.NewClient(baseURL, gitea.SetToken(token))
	if err != nil {
		return nil, err
	}
	return &Adapter{
		client: client,
	}, nil
}

func (g *Adapter) GetDiff(ctx context.Context, owner, repo string, prID int) (io.ReadCloser, error) {
	slog.Debug("Gitea GetDiff start", "owner", owner, "repo", repo, "pr", prID)
	g.client.SetContext(ctx)
	diff, _, err := g.client.GetPullRequestDiff(owner, repo, int64(prID), gitea.PullRequestDiffOptions{})
	if err != nil {
		return nil, err
	}
	rc := io.NopCloser(bytes.NewReader(diff))
	slog.Info("Gitea GetDiff success", "owner", owner, "repo", repo, "pr", prID, "bytes", len(diff))
	return rc, nil
}

func (g *Adapter) AddComment(ctx context.Context, owner, repo string, prID int, comment pierre.Comment) error {
	slog.Debug("Gitea AddComment start", "owner", owner, "repo", repo, "pr", prID, "file", comment.File, "line", comment.Line)
	g.client.SetContext(ctx)
	opts := gitea.CreatePullReviewOptions{
		State: gitea.ReviewStateComment,
		Comments: []gitea.CreatePullReviewComment{
			{
				Path:       comment.File,
				Body:       comment.Message,
				NewLineNum: int64(comment.Line),
			},
		},
	}
	_, _, err := g.client.CreatePullReview(owner, repo, int64(prID), opts)
	if err != nil {
		slog.Error("Gitea AddComment failed", "err", err)
		return err
	}
	slog.Info("Gitea AddComment succeeded", "pr", prID)
	return nil
}

// GetFileContent returns the file content at a given path and ref (commit SHA).
func (g *Adapter) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error) {
	slog.Debug("Gitea GetFileContent start", "owner", owner, "repo", repo, "path", path, "ref", ref)
	g.client.SetContext(ctx)
	// The SDK's GetFile returns the raw bytes of the file.
	data, _, err := g.client.GetFile(owner, repo, ref, path)
	if err != nil {
		return "", err
	}
	slog.Info("Gitea GetFileContent success", "bytes", len(data))
	return string(data), nil
}

// GetPRHeadSHA fetches the pull request and returns the head commit SHA.
func (g *Adapter) GetPRHeadSHA(ctx context.Context, owner, repo string, prID int) (string, error) {
	slog.Debug("Gitea GetPRHeadSHA start", "owner", owner, "repo", repo, "pr", prID)
	g.client.SetContext(ctx)
	// GetPullRequest returns the detailed PR information.
	pr, _, err := g.client.GetPullRequest(owner, repo, int64(prID))
	if err != nil {
		return "", err
	}
	if pr == nil || pr.Head == nil {
		return "", fmt.Errorf("pull request %d has no head information", prID)
	}
	slog.Info("Gitea GetPRHeadSHA success", "sha", pr.Head.Sha)
	return pr.Head.Sha, nil
}
