package localdb

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/glebarez/sqlite"
	"gorm.io/gorm"
)

func TestMigration006BackfillCreatesV1AndCurrentPointer(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "migration_backfill.db")
	db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}

	if err := db.Exec(`
CREATE TABLE local_configurations (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	uuid TEXT NOT NULL UNIQUE,
	server_id INTEGER NULL,
	name TEXT NOT NULL,
	items TEXT,
	total_price REAL,
	custom_price REAL,
	notes TEXT,
	is_template BOOLEAN DEFAULT FALSE,
	server_count INTEGER DEFAULT 1,
	price_updated_at DATETIME NULL,
	created_at DATETIME,
	updated_at DATETIME,
	synced_at DATETIME,
	sync_status TEXT DEFAULT 'local',
	original_user_id INTEGER DEFAULT 0,
	original_username TEXT DEFAULT ''
);`).Error; err != nil {
		t.Fatalf("create pre-migration schema: %v", err)
	}

	items := `[{"lot_name":"CPU_X","quantity":2,"unit_price":1000}]`
	now := time.Now().UTC().Format(time.RFC3339)
	if err := db.Exec(`
INSERT INTO local_configurations
	(uuid, name, items, total_price, notes, server_count, created_at, updated_at, sync_status, original_username)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
		"cfg-1", "Cfg One", items, 2000.0, "note", 1, now, now, "pending", "tester",
	).Error; err != nil {
		t.Fatalf("seed pre-migration data: %v", err)
	}

	migrationPath := filepath.Join("..", "..", "migrations", "006_add_local_configuration_versions.sql")
	sqlBytes, err := os.ReadFile(migrationPath)
	if err != nil {
		t.Fatalf("read migration file: %v", err)
	}
	if err := execSQLScript(db, string(sqlBytes)); err != nil {
		t.Fatalf("apply migration: %v", err)
	}

	var count int64
	if err := db.Table("local_configuration_versions").Where("configuration_uuid = ?", "cfg-1").Count(&count).Error; err != nil {
		t.Fatalf("count versions: %v", err)
	}
	if count != 1 {
		t.Fatalf("expected 1 version, got %d", count)
	}

	var currentVersionID *string
	if err := db.Table("local_configurations").Select("current_version_id").Where("uuid = ?", "cfg-1").Scan(&currentVersionID).Error; err != nil {
		t.Fatalf("read current_version_id: %v", err)
	}
	if currentVersionID == nil || *currentVersionID == "" {
		t.Fatalf("expected current_version_id to be set")
	}

	var row struct {
		ID        string
		VersionNo int
		Data      string
	}
	if err := db.Table("local_configuration_versions").
		Select("id, version_no, data").
		Where("configuration_uuid = ?", "cfg-1").
		First(&row).Error; err != nil {
		t.Fatalf("load v1 row: %v", err)
	}
	if row.VersionNo != 1 {
		t.Fatalf("expected version_no=1, got %d", row.VersionNo)
	}
	if row.ID != *currentVersionID {
		t.Fatalf("expected current_version_id=%s, got %s", row.ID, *currentVersionID)
	}

	var snapshot map[string]any
	if err := json.Unmarshal([]byte(row.Data), &snapshot); err != nil {
		t.Fatalf("parse snapshot json: %v", err)
	}
	if snapshot["uuid"] != "cfg-1" {
		t.Fatalf("expected snapshot uuid cfg-1, got %v", snapshot["uuid"])
	}
	if snapshot["name"] != "Cfg One" {
		t.Fatalf("expected snapshot name Cfg One, got %v", snapshot["name"])
	}
}

func execSQLScript(db *gorm.DB, script string) error {
	var cleaned []string
	for _, line := range strings.Split(script, "\n") {
		trimmed := strings.TrimSpace(line)
		if strings.HasPrefix(trimmed, "--") {
			continue
		}
		cleaned = append(cleaned, line)
	}

	for _, stmt := range strings.Split(strings.Join(cleaned, "\n"), ";") {
		sql := strings.TrimSpace(stmt)
		if sql == "" {
			continue
		}
		if err := db.Exec(sql).Error; err != nil {
			return err
		}
	}
	return nil
}
