package migrate

import (
	"github.com/pingcap/log"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

var _ = AddDBMigrateRecord(
	"202401221800",
	func(db *gorm.DB) error {
		ddl := "CREATE TABLE IF NOT EXISTS `exports` (" +
			"  `id` varchar(255) NOT NULL," +
			"  `state` enum('running','succeed','canceled','failed','deleted') NULL DEFAULT NULL," +
			"  `file_type` enum('SQL','CSV') NOT NULL DEFAULT 'SQL'," +
			"  `target_type` enum('LOCAL','S3') NOT NULL DEFAULT 'LOCAL'," +
			"  `s3_bucket_uri` varchar(255) NULL DEFAULT NULL," +
			"  `s3_access_key_id` varchar(255) NULL DEFAULT NULL," +
			"  `s3_secret_access_key` varchar(255) NULL DEFAULT NULL," +
			"  `local_url` longtext NULL DEFAULT NULL," +
			"  `csv_format` json NULL," +
			"  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
			"  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP," +
			"  `deleted_at` timestamp NULL DEFAULT NULL," +
			"  `tenant_id` varchar(255) NOT NULL," +
			"  `project_id` varchar(255) NOT NULL," +
			"  `cluster_id` varchar(255) NOT NULL," +
			"  `created_by` varchar(255) NOT NULL," +
			"  `filter` varchar(255) NULL DEFAULT NULL," +
			"  `metering_status` enum('pending','running','succeed') NOT NULL DEFAULT 'pending'," +
			"  `region` varchar(255) NOT NULL," +
			"  `completed_at` timestamp NULL DEFAULT NULL," +
			"  `reason` text NULL DEFAULT NULL," +
			"  PRIMARY KEY (`id`)," +
			"  INDEX `idx_cluster_id`(`cluster_id`)" +
			")"
		err := db.Exec(ddl).Error
		if err != nil {
			log.Error("create table exports failed", zap.Error(err))
		}
		return nil
	},
)
