package staticsite

import (
	"context"
	"errors"
	"testing"
)

// fakeReporterProvider is a stub GitProvider that records SetCommitStatus
// calls. Only the methods the reporter exercises are meaningful; the rest
// satisfy the interface and panic if ever hit so a mis-wired test is loud.
type fakeReporterProvider struct {
	calls   []reporterStatusCall
	failErr error // when set, SetCommitStatus returns it (best-effort path)
}

type reporterStatusCall struct {
	owner, repo, sha string
	status           CommitStatus
	targetURL, descr string
}

func (f *fakeReporterProvider) SetCommitStatus(_ context.Context, owner, repo, sha string, status CommitStatus, targetURL, description string) error {
	f.calls = append(f.calls, reporterStatusCall{owner, repo, sha, status, targetURL, description})
	return f.failErr
}

func (*fakeReporterProvider) Name() string { return "fake" }
func (*fakeReporterProvider) TestConnection(context.Context, string, string) error {
	panic("unused")
}
func (*fakeReporterProvider) ListRepos(context.Context, string) ([]RepoInfo, error) {
	panic("unused")
}
func (*fakeReporterProvider) ListBranches(context.Context, string, string) ([]string, error) {
	panic("unused")
}
func (*fakeReporterProvider) GetLatestCommitSHA(context.Context, string, string, string) (string, error) {
	panic("unused")
}
func (*fakeReporterProvider) ListTree(context.Context, string, string, string) ([]FolderEntry, error) {
	panic("unused")
}
func (*fakeReporterProvider) DownloadFolder(context.Context, string, string, string, string, string) error {
	panic("unused")
}
func (*fakeReporterProvider) DownloadFile(context.Context, string, string, string, string, int64) ([]byte, error) {
	panic("unused")
}

// Enabled: forwards to the provider with the captured identifiers + target.
func TestCommitStatusReporter_Enabled_Calls(t *testing.T) {
	fp := &fakeReporterProvider{}
	r := NewCommitStatusReporter(fp, "owner", "pages", "abc123", "https://app.example.com", true)

	r.Report(context.Background(), "site", "wid-1", CommitStatusPending, "Tinyforge: deploying")
	r.Report(context.Background(), "site", "wid-1", CommitStatusSuccess, "Tinyforge: deployed")

	if len(fp.calls) != 2 {
		t.Fatalf("calls = %d, want 2", len(fp.calls))
	}
	first := fp.calls[0]
	if first.owner != "owner" || first.repo != "pages" || first.sha != "abc123" {
		t.Errorf("identifiers wrong: %+v", first)
	}
	if first.status != CommitStatusPending {
		t.Errorf("first status = %q, want pending", first.status)
	}
	if first.targetURL != "https://app.example.com" {
		t.Errorf("targetURL = %q", first.targetURL)
	}
	if fp.calls[1].status != CommitStatusSuccess {
		t.Errorf("second status = %q, want success", fp.calls[1].status)
	}
}

// Disabled: the reporter is inert.
func TestCommitStatusReporter_Disabled_NoCalls(t *testing.T) {
	fp := &fakeReporterProvider{}
	r := NewCommitStatusReporter(fp, "owner", "pages", "abc123", "", false)

	r.Report(context.Background(), "site", "wid-1", CommitStatusSuccess, "x")
	if len(fp.calls) != 0 {
		t.Fatalf("expected no calls when disabled, got %d", len(fp.calls))
	}
}

// An empty SHA (e.g. a provider that couldn't resolve the branch) must not
// produce a status call even when reporting is enabled.
func TestCommitStatusReporter_EmptySHA_NoCalls(t *testing.T) {
	fp := &fakeReporterProvider{}
	r := NewCommitStatusReporter(fp, "owner", "pages", "", "", true)

	r.Report(context.Background(), "site", "wid-1", CommitStatusPending, "x")
	if len(fp.calls) != 0 {
		t.Fatalf("expected no calls with empty SHA, got %d", len(fp.calls))
	}
}

// A provider error must be swallowed (best-effort) — Report never panics or
// propagates. We assert it returns normally after a failing provider call.
func TestCommitStatusReporter_ProviderError_Swallowed(t *testing.T) {
	fp := &fakeReporterProvider{failErr: errors.New("boom")}
	r := NewCommitStatusReporter(fp, "owner", "pages", "abc123", "", true)

	// Should not panic / propagate.
	r.Report(context.Background(), "site", "wid-1", CommitStatusFailure, "Tinyforge: deploy failed")
	if len(fp.calls) != 1 {
		t.Fatalf("expected the failing call to still be recorded, got %d", len(fp.calls))
	}
}

// A nil reporter (constructed only when needed in some call paths) is safe.
func TestCommitStatusReporter_NilSafe(t *testing.T) {
	var r *CommitStatusReporter
	// Must not panic.
	r.Report(context.Background(), "site", "wid-1", CommitStatusSuccess, "x")
}

// A nil provider on an enabled reporter is also a no-op (defensive guard).
func TestCommitStatusReporter_NilProvider_NoPanic(t *testing.T) {
	r := NewCommitStatusReporter(nil, "owner", "pages", "abc123", "", true)
	// Must not panic.
	r.Report(context.Background(), "site", "wid-1", CommitStatusSuccess, "x")
}
