package test

import (
	"github.com/pingcap/dbaas/billing2/db"
	"github.com/pingcap/dbaas/billing2/db/entity"
	"github.com/pingcap/dbaas/billing2/handler/workerhandler"
	"github.com/pingcap/dbaas/billing2/utils/tests"
	"github.com/pingcap/dbaas/billing2/utils/timeutils"
	"github.com/stretchr/testify/suite"
	"testing"
	"time"
)

type DupWorkerSuite struct {
	tests.BillingTestSuite
}

func TestDuplicateWorkerSuite(t *testing.T) {
	suite.Run(t, new(DupWorkerSuite))
}

func (suite *DupWorkerSuite) TestWorkerDuplicateCheckAndInsert() {
	assert := suite.Assert()
	const tenantID uint = 1
	monthStartTime := timeutils.GetLastMonth(timeutils.GetNow())
	workerKey, isDuplicate := workerhandler.Get().WorkerDuplicateCheck(tenantID, "SyncMarketOrgBills", timeutils.MONTH, monthStartTime)
	assert.False(isDuplicate)
	err := workerhandler.Get().WorkerRunningRecordInsertDb(tenantID, "SyncMarketOrgBills", workerKey, nil)
	assert.NoError(err)
	_, isDuplicate = workerhandler.Get().WorkerDuplicateCheck(tenantID, "SyncMarketOrgBills", timeutils.MONTH, monthStartTime)
	assert.True(isDuplicate)

}
func (suite *DupWorkerSuite) TestDedupeRun() {
	assert := suite.Assert()
	const tenantID uint = 1
	monthStartTime := timeutils.GetLastMonth(timeutils.GetNow())
	success := workerhandler.Get().DedupeRun(tenantID, "SyncMarketOrgBills", timeutils.MONTH, monthStartTime, DealAndSync)

	assert.True(success)
	_, isDuplicate := workerhandler.Get().WorkerDuplicateCheck(tenantID, "SyncMarketOrgBills", timeutils.MONTH, monthStartTime)

	assert.True(isDuplicate)

}

// TestAREstimatedDedupeKeyNoCollision simulates a deploy boundary where a
// previous code revision wrote a dedupe record using the execution day as the
// key component (the pre-#1232 behaviour). After the fix that restores
// AREstimatedWorker's dedupe key to the execution day, a natural monthly tick
// on the next 1st of month must NOT be flagged as a duplicate.
//
// Background: this test pins down the regression that surfaced on 2026-07-01
// where the natural tick was dedupe-skipped because the previous-code record
// ("AREstimatedWorker-month-0-2026-06 ") collided with the post-#1232 key
// derived from the previous-month billing date (which also formatted to
// "2026-06").
func (suite *DupWorkerSuite) TestAREstimatedDedupeKeyNoCollision() {
	assert := suite.Assert()
	const tenantID uint = 0

	// Simulate the legacy 2026-06-01 tick having inserted a record using the
	// execution-day key format (the pre-#1232 behaviour).
	legacyKey := "AREstimatedWorker-month-0-2026-06 "
	legacy := &entity.WorkerRunningRecord{
		TenantID:   tenantID,
		WorkerName: "AREstimatedWorker",
		WorkerKey:  legacyKey,
	}
	err := legacy.Create(db.GetDB())
	assert.NoError(err)

	// 2026-07-01 natural tick: dedupe key is computed from the execution day
	// (after the fix), so it should format to "2026-07" and NOT collide with
	// the legacy "2026-06" record.
	tick := time.Date(2026, 7, 1, 0, 30, 0, 0, time.UTC)
	_, isDuplicate := workerhandler.Get().WorkerDuplicateCheck(tenantID, "AREstimatedWorker", timeutils.MONTH, tick)
	assert.False(isDuplicate, "natural tick on 2026-07-01 must not be masked by the legacy 2026-06 record")

	// Cleanup.
	_, err = entity.NewWorkerRunningRecordQuerySet(db.GetDB()).WorkerKeyEq(legacyKey).DeleteNum()
	assert.NoError(err)
}

func DealAndSync(tenantID uint, billsStart time.Time) (interface{}, error) {
	return nil, nil
}
