package copilot

import (
	"context"
	"fmt"
	"net/http"
	"time"

	go_pkg_http "github.com/pardnchiu/go-pkg/http"
)

const (
	copilotTokenAPI = "https://api.github.com/copilot_internal/v2/token"
)

type RefreshToken struct {
	Token     string `json:"token"`
	ExpiresAt int64  `json:"expires_at"`
}

func (c *Agent) checkExpires(ctx context.Context) error {
	if c.Refresh == nil || time.Now().Unix() >= c.Refresh.ExpiresAt-60 {
		return c.refresh(ctx)
	}
	return nil
}

func (c *Agent) refresh(ctx context.Context) error {
	token, code, err := go_pkg_http.GET[RefreshToken](ctx, nil, copilotTokenAPI, map[string]string{
		"Authorization":  "token " + c.Token.AccessToken,
		"Accept":         "application/json",
		"Editor-Version": "vscode/1.95.0",
	})
	if err != nil {
		return fmt.Errorf("http.GET: %w", err)
	}
	if code == http.StatusUnauthorized {
		return fmt.Errorf("copilot token expired; run `agen model add` to re-authenticate")
	}
	if code == http.StatusForbidden || code == http.StatusNotFound {
		return fmt.Errorf("http.GET: token refresh failed")
	}
	if code != http.StatusOK {
		return fmt.Errorf("http.GET: %d", code)
	}

	c.Refresh = &token

	return nil
}
