package controlplane

import (
	"context"
	"regexp"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
	database "github.com/nexuscrm/db"
)

func TestContentCompatStoragePathUsesNeutralContentVersionID(t *testing.T) {
	got := contentCompatStoragePath(&contentCompatVersionRow{LatestVersionID: " 068-test "})
	if got != "068-test" {
		t.Fatalf("contentCompatStoragePath = %q", got)
	}
}

func TestBackfillContentCompatSystemFilesSkipsWhenLegacyLinkTableMissing(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("sqlmock.New: %v", err)
	}
	defer db.Close()

	mock.ExpectQuery(regexp.QuoteMeta(
		"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES " +
			"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?",
	)).
		WithArgs("ContentDocumentLink").
		WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow(0))

	if err := BackfillContentCompatSystemFiles(context.Background(), database.NewConnectionFromDB(db)); err != nil {
		t.Fatalf("BackfillContentCompatSystemFiles: %v", err)
	}
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Fatalf("sql expectations: %v", err)
	}
}
