package commands

import (
	"strings"
	"testing"

	"c65gm/internal/compiler"
	"c65gm/internal/preproc"
)

func TestOrCommand_WillHandle(t *testing.T) {
	cmd := &OrCommand{}

	tests := []struct {
		name string
		line string
		want bool
	}{
		{"old syntax WITH/GIVING", "OR a WITH b GIVING c", true},
		{"old syntax WITH/arrow", "OR a WITH b -> c", true},
		{"new syntax", "result = x | y", true},
		{"not OR", "ADD a TO b GIVING c", false},
		{"wrong param count", "OR a b c", false},
		{"empty", "", false},
		{"new syntax wrong op", "result = x + y", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			line := preproc.Line{Text: tt.line, Kind: preproc.Source}
			got := cmd.WillHandle(line)
			if got != tt.want {
				t.Errorf("WillHandle() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestOrCommand_OldSyntax(t *testing.T) {
	tests := []struct {
		name      string
		line      string
		setupVars func(*compiler.SymbolTable)
		wantAsm   []string
		wantErr   bool
	}{
		{
			name: "byte OR byte -> byte (variables)",
			line: "OR a WITH b GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora b",
				"\tsta result",
			},
		},
		{
			name: "byte OR byte -> word",
			line: "OR a WITH b GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora b",
				"\tsta result",
				"\tlda #0",
				"\tsta result+1",
			},
		},
		{
			name: "word OR word -> word",
			line: "OR x WITH y GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("x", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("y", "", compiler.KindWord, 0x0F0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda x",
				"\tora y",
				"\tsta result",
				"\tlda x+1",
				"\tora y+1",
				"\tsta result+1",
			},
		},
		{
			name: "byte OR literal -> byte",
			line: "OR a WITH $0F GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora #$0f",
				"\tsta result",
			},
		},
		{
			name: "literal OR byte -> byte",
			line: "OR 15 WITH b GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$0f",
				"\tora b",
				"\tsta result",
			},
		},
		{
			name: "constant folding: 15 OR 240 -> byte",
			line: "OR 15 WITH 240 GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
			},
		},
		{
			name: "constant folding: $00F0 OR $0F00 -> word",
			line: "OR $00F0 WITH $0F00 GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$f0",
				"\tsta result",
				"\tlda #$0f",
				"\tsta result+1",
			},
		},
		{
			name: "arrow syntax",
			line: "OR a WITH b -> result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora b",
				"\tsta result",
			},
		},
		{
			name: "word OR byte -> byte",
			line: "OR wval WITH bval GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("bval", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta result",
			},
		},
		{
			name: "word OR byte -> word",
			line: "OR wval WITH bval GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("bval", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta result",
				"\tlda wval+1",
				"\tsta result+1",
			},
		},
		{
			name: "error: unknown destination variable",
			line: "OR a WITH b GIVING unknown",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantErr: true,
		},
		{
			name: "error: wrong separator",
			line: "OR a TO b GIVING result",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantErr: true,
		},
		{
			name: "error: cannot assign to constant",
			line: "OR a WITH b GIVING MAXVAL",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddConst("MAXVAL", "", compiler.KindByte, 255, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx := compiler.NewCompilerContext(&preproc.Pragma{})
			tt.setupVars(ctx.SymbolTable)

			cmd := &OrCommand{}
			line := preproc.Line{Text: tt.line, Kind: preproc.Source}

			err := cmd.Interpret(line, ctx)
			if tt.wantErr {
				if err == nil {
					t.Error("expected error, got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("Interpret() error = %v", err)
			}

			asm, err := cmd.Generate(ctx)
			if err != nil {
				t.Fatalf("Generate() error = %v", err)
			}

			if !equalAsmOr(asm, tt.wantAsm) {
				t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
					strings.Join(asm, "\n"),
					strings.Join(tt.wantAsm, "\n"))
			}
		})
	}
}

func TestOrCommand_NewSyntax(t *testing.T) {
	tests := []struct {
		name      string
		line      string
		setupVars func(*compiler.SymbolTable)
		wantAsm   []string
		wantErr   bool
	}{
		{
			name: "byte | byte -> byte",
			line: "result = a | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora b",
				"\tsta result",
			},
		},
		{
			name: "byte | byte -> word",
			line: "result = a | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora b",
				"\tsta result",
				"\tlda #0",
				"\tsta result+1",
			},
		},
		{
			name: "word | word -> word",
			line: "result = x | y",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("x", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("y", "", compiler.KindWord, 0x0F0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda x",
				"\tora y",
				"\tsta result",
				"\tlda x+1",
				"\tora y+1",
				"\tsta result+1",
			},
		},
		{
			name: "variable | literal",
			line: "result = a | $0F",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora #$0f",
				"\tsta result",
			},
		},
		{
			name: "byte | 0 -> byte (optimization: skip ora #0)",
			line: "result = a | 0",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tsta result",
			},
		},
		{
			name: "byte | 0 -> word (optimization: skip ora #0)",
			line: "result = a | 0",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tsta result",
				"\tlda #0",
				"\tsta result+1",
			},
		},
		{
			name: "constant folding",
			line: "result = 15 | 240",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
			},
		},
		{
			name: "constant folding word",
			line: "result = $00F0 | $0F00",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$f0",
				"\tsta result",
				"\tlda #$0f",
				"\tsta result+1",
			},
		},
		{
			name: "using constant in expression",
			line: "result = a | BITS",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddConst("BITS", "", compiler.KindByte, 0x0F, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tora #$0f",
				"\tsta result",
			},
		},
		{
			name: "byte | $FF -> byte (optimization: lda #$ff)",
			line: "result = a | $FF",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
			},
		},
		{
			name: "$FF | byte -> byte (optimization: lda #$ff)",
			line: "result = $FF | a",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
			},
		},
		{
			name: "byte | $FF -> word (optimization: lda #$ff)",
			line: "result = a | $FF",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
				"\tlda #0",
				"\tsta result+1",
			},
		},
		{
			name: "byte | byte -> byte (same variable: a | a = a)",
			line: "result = a | a",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tsta result",
			},
		},
		{
			name: "0 | byte -> word (optimization: skip ora #0)",
			line: "result = 0 | a",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0xF0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda a",
				"\tsta result",
				"\tlda #$00",
				"\tsta result+1",
			},
		},
		{
			name: "byte | word -> byte (swap case)",
			line: "result = bval | wval",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("bval", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta result",
			},
		},
		{
			name: "byte | word -> word (swap case)",
			line: "result = bval | wval",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("bval", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta result",
				"\tlda wval+1",
				"\tsta result+1",
			},
		},
		{
			name: "$FF | word -> word",
			line: "result = $FF | wval",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
				"\tlda wval+1",
				"\tsta result+1",
			},
		},
		{
			name: "word | $FF -> word",
			line: "result = wval | $FF",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$ff",
				"\tsta result",
				"\tlda wval+1",
				"\tsta result+1",
			},
		},
		{
			name: "$FF00 | byte -> byte",
			line: "result = $FF00 | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xAB, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda b",
				"\tsta result",
			},
		},
		{
			name: "$FF00 | byte -> word",
			line: "result = $FF00 | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xAB, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda b",
				"\tsta result",
				"\tlda #$ff",
				"\tsta result+1",
			},
		},
		{
			name: "byte | word_const -> byte",
			line: "result = b | 300",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda b",
				"\tora #$2c",
				"\tsta result",
			},
		},
		{
			name: "byte | word_const -> word",
			line: "result = b | 300",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda b",
				"\tora #$2c",
				"\tsta result",
				"\tlda #0",
				"\tora #$01",
				"\tsta result+1",
			},
		},
		{
			name: "word_const | byte -> byte",
			line: "result = 300 | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$2c",
				"\tora b",
				"\tsta result",
			},
		},
		{
			name: "word_const | byte -> word",
			line: "result = 300 | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("b", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda #$2c",
				"\tora b",
				"\tsta result",
				"\tlda #$01",
				"\tsta result+1",
			},
		},
		{
			name: "self-assignment: word |= byte (optimization: skip high byte entirely)",
			line: "wval = wval | bval",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("bval", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta wval",
			},
		},
		{
			name: "self-assignment reversed: word |= byte (optimization via swap)",
			line: "wval = bval | wval",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("bval", "", compiler.KindByte, 0xFF, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora bval",
				"\tsta wval",
			},
		},
		{
			name: "self-assignment: word |= byte_const (optimization: skip high byte entirely)",
			line: "wval = wval | 42",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora #$2a",
				"\tsta wval",
			},
		},
		{
			name: "self-assignment: word |= word_const (no optimization: high byte needed)",
			line: "wval = wval | 300",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora #$2c",
				"\tsta wval",
				"\tlda wval+1",
				"\tora #$01",
				"\tsta wval+1",
			},
		},
		{
			name: "self-assignment: word |= word (no optimization: both high bytes needed)",
			line: "wval = wval | wval2",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("wval", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("wval2", "", compiler.KindWord, 0x5678, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda wval",
				"\tora wval2",
				"\tsta wval",
				"\tlda wval+1",
				"\tora wval2+1",
				"\tsta wval+1",
			},
		},
		{
			name: "word | word -> word (same variable: x | x = x)",
			line: "result = x | x",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("x", "", compiler.KindWord, 0x1234, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindWord, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantAsm: []string{
				"\tlda x",
				"\tsta result",
				"\tlda x+1",
				"\tsta result+1",
			},
		},
		{
			name: "error: unknown destination",
			line: "unknown = a | b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantErr: true,
		},
		{
			name: "error: wrong operator",
			line: "result = a + b",
			setupVars: func(st *compiler.SymbolTable) {
				st.AddVar("a", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("b", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
				st.AddVar("result", "", compiler.KindByte, 0, preproc.Line{Filename: "test.c65", LineNo: 1})
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx := compiler.NewCompilerContext(&preproc.Pragma{})
			tt.setupVars(ctx.SymbolTable)

			cmd := &OrCommand{}
			line := preproc.Line{Text: tt.line, Kind: preproc.Source}

			err := cmd.Interpret(line, ctx)
			if tt.wantErr {
				if err == nil {
					t.Error("expected error, got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("Interpret() error = %v", err)
			}

			asm, err := cmd.Generate(ctx)
			if err != nil {
				t.Fatalf("Generate() error = %v", err)
			}

			if !equalAsmOr(asm, tt.wantAsm) {
				t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
					strings.Join(asm, "\n"),
					strings.Join(tt.wantAsm, "\n"))
			}
		})
	}
}

// equalAsmOr compares two assembly slices for equality
func equalAsmOr(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}
