package main

import (
	"testing"

	"system-altrak/internal/migration"
)

func TestTableRenameStepsIncludeCanonicalLegacyMappings(t *testing.T) {
	steps := make(map[string]string, len(migration.TableRenameSteps))
	statusPoIndex := -1
	poStatusIndex := -1

	for index, step := range migration.TableRenameSteps {
		steps[step.OldName] = step.NewName
		if step.OldName == "status_po" {
			statusPoIndex = index
		}
		if step.OldName == "po_status" {
			poStatusIndex = index
		}
	}

	if got := steps["document_sequences"]; got != "serial_sequences" {
		t.Fatalf("expected document_sequences to rename to serial_sequences, got %q", got)
	}

	if got := steps["documents"]; got != "operational_records" {
		t.Fatalf("expected documents to rename to operational_records, got %q", got)
	}

	if statusPoIndex < 0 || poStatusIndex < 0 {
		t.Fatal("expected status_po and po_status rename steps to exist")
	}

	if statusPoIndex >= poStatusIndex {
		t.Fatalf("expected status_po rename to run before po_status rename, got %d then %d", statusPoIndex, poStatusIndex)
	}
}

func TestColumnRenameMappingIncludesCanonicalColumns(t *testing.T) {
	operationalRecordCols, ok := migration.ColumnRenameMapping["operational_records"]
	if !ok {
		t.Fatal("expected operational_records column mapping to exist")
	}
	if got := operationalRecordCols["document_status"]; got != "record_status" {
		t.Fatalf("expected document_status to rename to record_status, got %q", got)
	}

	srCols, ok := migration.ColumnRenameMapping["service_requisitions"]
	if !ok {
		t.Fatal("expected service_requisitions column mapping to exist")
	}
	if got := srCols["spjb_no"]; got != "sales_agreement_no" {
		t.Fatalf("expected spjb_no to rename to sales_agreement_no, got %q", got)
	}
}
