package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"

	storemongo "scheduler-backend/internal/store/mongo"
	"scheduler-backend/pkg/config"
	"scheduler-backend/pkg/log"
)

func main() {
	cfg := config.Load()
	logger := log.New()

	databases, err := storemongo.Connect(context.Background(), cfg)
	if err != nil {
		logger.Error("seed sample job mongo connect failed", "error", err)
		os.Exit(1)
	}
	defer func() {
		_ = databases.Client.Disconnect(context.Background())
	}()

	now := time.Now()
	doc := bson.M{
		"name":          "sample-job",
		"handlerKey":    "sample-handler",
		"enabled":       false,
		"scheduleType":  "manual",
		"scheduleValue": "",
		"defaultParams": `{"limit":10,"dryRun":true}`,
		"lastStatus":    "idle",
		"updatedAt":     now,
	}

	result, err := databases.MetaDB.Collection("job_configs").UpdateOne(
		context.Background(),
		bson.M{"name": "sample-job"},
		bson.M{
			"$set":         doc,
			"$setOnInsert": bson.M{"createdAt": now},
		},
		options.Update().SetUpsert(true),
	)
	if err != nil {
		logger.Error("seed sample job failed", "error", err)
		os.Exit(1)
	}

	logger.Info(
		"seed sample job completed",
		"matched",
		result.MatchedCount,
		"modified",
		result.ModifiedCount,
		"upserted",
		fmt.Sprintf("%v", result.UpsertedID),
	)
}
