// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2

package types

import "testing"

func TestTxOutToKey_TargetType_Good(t *testing.T) {
	var target TxOutTarget = TxOutToKey{Key: PublicKey{1}, MixAttr: 0}
	if target.TargetType() != TargetTypeToKey {
		t.Errorf("TargetType: got %d, want %d", target.TargetType(), TargetTypeToKey)
	}
}

func TestAsTxOutToKey_Good(t *testing.T) {
	target, ok := AsTxOutToKey(TxOutToKey{Key: PublicKey{1}, MixAttr: 7})
	if !ok {
		t.Fatal("AsTxOutToKey: expected true for TxOutToKey target")
	}
	if target.Key != (PublicKey{1}) {
		t.Errorf("Key: got %x, want %x", target.Key, PublicKey{1})
	}
	if target.MixAttr != 7 {
		t.Errorf("MixAttr: got %d, want %d", target.MixAttr, 7)
	}
}

func TestAsTxOutToKey_Bad(t *testing.T) {
	if _, ok := AsTxOutToKey(TxOutHTLC{}); ok {
		t.Fatal("AsTxOutToKey: expected false for non-to-key target")
	}
}

func TestTxOutputBare_SpendKey_Good(t *testing.T) {
	out := TxOutputBare{
		Amount: 10,
		Target: TxOutToKey{Key: PublicKey{0xAA, 0xBB}, MixAttr: 3},
	}
	key, ok := out.SpendKey()
	if !ok {
		t.Fatal("SpendKey: expected true for TxOutToKey target")
	}
	if key != (PublicKey{0xAA, 0xBB}) {
		t.Errorf("SpendKey: got %x, want %x", key, PublicKey{0xAA, 0xBB})
	}
}

func TestTxOutputBare_SpendKey_Bad(t *testing.T) {
	out := TxOutputBare{Target: TxOutHTLC{Expiration: 100}}
	if key, ok := out.SpendKey(); ok || key != (PublicKey{}) {
		t.Fatalf("SpendKey: got (%x, %v), want zero key and false", key, ok)
	}
}

func TestTxOutMultisig_TargetType_Good(t *testing.T) {
	var target TxOutTarget = TxOutMultisig{MinimumSigs: 2, Keys: []PublicKey{{1}, {2}}}
	if target.TargetType() != TargetTypeMultisig {
		t.Errorf("TargetType: got %d, want %d", target.TargetType(), TargetTypeMultisig)
	}
}

func TestTxOutHTLC_TargetType_Good(t *testing.T) {
	var target TxOutTarget = TxOutHTLC{
		Flags:      0,
		Expiration: 10080,
	}
	if target.TargetType() != TargetTypeHTLC {
		t.Errorf("TargetType: got %d, want %d", target.TargetType(), TargetTypeHTLC)
	}
}

func TestTxInputHTLC_InputType_Good(t *testing.T) {
	var input TxInput = TxInputHTLC{
		HTLCOrigin: "test",
		Amount:     1000,
		KeyImage:   KeyImage{1},
	}
	if input.InputType() != InputTypeHTLC {
		t.Errorf("InputType: got %d, want %d", input.InputType(), InputTypeHTLC)
	}
}

func TestTxInputMultisig_InputType_Good(t *testing.T) {
	var input TxInput = TxInputMultisig{
		Amount:    500,
		SigsCount: 2,
	}
	if input.InputType() != InputTypeMultisig {
		t.Errorf("InputType: got %d, want %d", input.InputType(), InputTypeMultisig)
	}
}
