// Copyright (c) 2026 Alibaba Group and its affiliates

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package transparency

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewTranslogServiceRekorV1(t *testing.T) {
	svc, err := NewTranslogService(LogTypeRekorV1, &RekorV1ServiceConfig{})
	require.NoError(t, err)
	assert.NotNil(t, svc)
}

func TestNewTranslogServiceNilConfigError(t *testing.T) {
	svc, err := NewTranslogService(LogTypeRekorV1, nil)
	assert.Nil(t, svc)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "requires *RekorV1ServiceConfig")
}

func TestNewTranslogServiceUnsupportedType(t *testing.T) {
	svc, err := NewTranslogService("unknown", nil)
	assert.Nil(t, svc)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "unsupported log type")
}

func TestNewTranslogServiceBadConfig(t *testing.T) {
	svc, err := NewTranslogService(LogTypeRekorV1, "bad-config")
	assert.Nil(t, svc)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "expects *RekorV1ServiceConfig")
}

func TestFetchProofEntryLogURLRequired(t *testing.T) {
	svc, err := NewTranslogService(LogTypeRekorV1, &RekorV1ServiceConfig{})
	require.NoError(t, err)

	_, err = svc.FetchProofEntry(context.Background(), 0)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "log URL is required")
}

func TestLogTypeConstants(t *testing.T) {
	assert.Equal(t, LogType("rekor-v1"), LogTypeRekorV1)
}

func TestDSSEPayloadTypeConstant(t *testing.T) {
	assert.Equal(t, "application/vnd.modelstudio.cmaas.release+json", DSSEPayloadType)
}

func TestDefaultKeyConstants(t *testing.T) {
	assert.Contains(t, RekorV1PubKeyPEM, "BEGIN PUBLIC KEY")
	assert.Contains(t, LogEntryPubKeyPEM, "BEGIN PUBLIC KEY")
}

func TestReleaseManifestJSON(t *testing.T) {
	output := map[string]interface{}{
		"release_manifest": map[string]interface{}{
			"schemaVersion": float64(1),
			"measurements": map[string]interface{}{
				"cvm_uki": map[string]interface{}{"algorithm": "sha384", "value": "abc123"},
			},
		},
		"transparency_proof": []interface{}{
			map[string]interface{}{"type": "rekor-v1", "data": map[string]interface{}{"logUrl": "https://rekor.sigstore.dev", "logIndex": float64(123)}},
		},
	}

	manifestBytes, err := json.Marshal(output["release_manifest"])
	require.NoError(t, err)

	var manifest ReleaseManifest
	require.NoError(t, json.Unmarshal(manifestBytes, &manifest))
	assert.Equal(t, 1, manifest.SchemaVersion)
	assert.Len(t, manifest.Measurements, 1)
	assert.Equal(t, "sha384", manifest.Measurements["cvm_uki"].Algorithm)

	proofBytes, err := json.Marshal(output["transparency_proof"])
	require.NoError(t, err)

	var proofs []ProofEntry
	require.NoError(t, json.Unmarshal(proofBytes, &proofs))
	assert.Len(t, proofs, 1)
	assert.Equal(t, "rekor-v1", proofs[0].Type)
}
