package iface

import (
	"context"

	"github.com/tidbcloud/observability/o11ymetering/pkg/common/constants"
	"github.com/tidbcloud/observability/o11ymetering/pkg/common/models"
)

type StatisticReaderWriter interface {
	GetTask(ctx context.Context, id string) (*models.StatisticTask, error)
	BatchCreateTask(ctx context.Context, tasks []*models.StatisticTask) ([]*models.StatisticTask, error)
	UpdateTaskState(ctx context.Context, id string, originalState constants.StatisticState, state constants.StatisticState) error
	DeleteTasksByPeriod(ctx context.Context, periods []string) ([]*models.StatisticTask, error)
	GetLastedTaskPeriod(ctx context.Context, region string) (string, error)

	PickEarliestPendingTasks(ctx context.Context, region string, maxTasks int) ([]*models.StatisticTask, error)

	SaveStatisticRecordsForTask(ctx context.Context, taskID string, records []*models.StatisticRecord) ([]*models.StatisticRecord, error)
	DeleteRecordsByPeriod(ctx context.Context, periods []string) ([]*models.StatisticRecord, error)
}

type StatisticMergeReaderWriter interface {
	CopyRecords(ctx context.Context, source string, region string) error
	LatestMergedRecordPeriod(ctx context.Context, region string) (string, error)
	MonthlyStatisticSum(ctx context.Context, region string, startPeriod string, endPeriod string, statisticName constants.StatisticName, category constants.Category) (float64, error)
	MonthlyTenantStatistic(ctx context.Context, region string, startPeriod string, endPeriod string, statisticName constants.StatisticName) ([]*models.MonthlyTenantStatistic, error)
}

type StatisticReportReaderWriter interface {
	IfReportExists(ctx context.Context, timestamp int32, region string) (bool, error)
	IfCostReady(ctx context.Context, timestamp int32, region string) (bool, error)
	GetMonthlyCost(ctx context.Context, region string, timestamp int32, cloudService string) (float64, error)

	InsertReport(ctx context.Context, reports []*models.MonthlyStatisticReport) error
	CalculateMonthlyProportionSum(ctx context.Context, targetTimestamp int64, targetMonth string) ([]models.MonthlyProportionSum, error)
}

type ProportionReaderWriter interface {
	InsertProportion(ctx context.Context, proportions []*models.MonthlyProportion) error
}
