package pimport

// This file contains codes copy from lightning
// some define in lighting is not exported, so we just copy here and avoid depend on lighting repo
//
// To create a task:
// curl -X POST  --data-binary '@tidb-lightning.toml' localhost:8289/tasks

// Get task info
// path: /progress/task
// https://github.com/pingcap/tidb-lightning/blob/bce9977e455cd7ea3b11bf015dd2da7f5761ff4f/lightning/web/progress.go#L103
// Table info
// path: /progress/table?t={table_name}
// https://github.com/pingcap/tidb-lightning/blob/bce9977e455cd7ea3b11bf015dd2da7f5761ff4f/lightning/checkpoints/checkpoints.go#L162

type taskStatus uint8

const (
	taskStatusNotStarted taskStatus = 0
	taskStatusRunning    taskStatus = 1
	taskStatusCompleted  taskStatus = 2
)

type tableInfo struct {
	TotalWritten int64      `json:"w"`
	TotalSize    int64      `json:"z"`
	Status       taskStatus `json:"s"`
	Message      string     `json:"m,omitempty"`
}

type TaskProgress struct {
	Tables  map[string]*tableInfo `json:"t"`
	Status  taskStatus            `json:"s"`
	Message string                `json:"m,omitempty"`
}

type CheckpointStatus uint8

const (
	CheckpointStatusMissing         CheckpointStatus = 0
	CheckpointStatusMaxInvalid      CheckpointStatus = 25
	CheckpointStatusLoaded          CheckpointStatus = 30
	CheckpointStatusAllWritten      CheckpointStatus = 60
	CheckpointStatusClosed          CheckpointStatus = 90
	CheckpointStatusImported        CheckpointStatus = 120
	CheckpointStatusIndexImported   CheckpointStatus = 140
	CheckpointStatusAlteredAutoInc  CheckpointStatus = 150
	CheckpointStatusChecksumSkipped CheckpointStatus = 170
	CheckpointStatusChecksummed     CheckpointStatus = 180
	CheckpointStatusAnalyzeSkipped  CheckpointStatus = 200
	CheckpointStatusAnalyzed        CheckpointStatus = 210
)

type TableCheckpoint struct {
	Status    CheckpointStatus
	AllocBase int64
	Engines   map[int32]*EngineCheckpoint
	TableID   int64
}

type SourceFileMeta struct {
	Path string
	// Type        SourceType
	// Compression Compression
	SortKey string
}

// Chunk represents a portion of the data file.
type Chunk struct {
	Offset       int64
	EndOffset    int64
	PrevRowIDMax int64
	RowIDMax     int64
	Columns      []string
}

type ChunkCheckpointKey struct {
	Path   string
	Offset int64
}

type ChunkCheckpoint struct {
	Key               ChunkCheckpointKey
	FileMeta          SourceFileMeta
	ColumnPermutation []int
	Chunk             Chunk
	// Checksum          verify.KVChecksum
	Timestamp int64
}

type EngineCheckpoint struct {
	Status CheckpointStatus
	Chunks []*ChunkCheckpoint // a sorted array
}
