package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
)

// TestResultBES holds the schema definition for the TestResultBES entity.
type TestResultBES struct {
	ent.Schema
}

// Fields of the TestResult.
func (TestResultBES) Fields() []ent.Field {
	return []ent.Field{
		// The status of this test.
		field.Enum("test_status").Optional().
			Values("NO_STATUS",
				"PASSED",
				"FLAKY",
				"TIMEOUT",
				"FAILED",
				"INCOMPLETE",
				"REMOTE_FAILURE",
				"FAILED_TO_BUILD",
				"TOOL_HALTED_BEFORE_TESTING").
			Default("NO_STATUS"),

		// Status Details.
		// Additional details about the status of the test. This is intended for
		// user display and must not be parsed.
		field.String("status_details").Optional(),

		// The label.
		// possibly redundant, could remove, but this is the label associated with this test
		field.String("label").Optional(),

		// Warnings generated by that test action.
		field.Strings("warning").Optional(),

		// Cached locally.
		// True, if the reported attempt is taken from the tool's local cache.
		field.Bool("cached_locally").Optional(),

		// Time in milliseconds since the epoch at which the test attempt was started.
		// Note: for cached test results, this is time can be before the start of the
		// build.
		// Deprecated, use `test_attempt_start` instead.
		field.Int64("test_attempt_start_millis_epoch").Optional(),

		// Time at which the test attempt was started.
		// Note: for cached test results, this is time can be before the start of the
		// build. (Timestamp)
		field.String("test_attempt_start").Optional(),

		// Time the test took to run. For locally cached results.
		// this is the time
		// the cached invocation took when it was invoked.
		// Deprecated, use `test_attempt_duration` instead.
		field.Int64("test_attempt_duration_millis").Optional(),

		// Time the test took to run. For locally cached results.
		// this is the time
		// the cached invocation took when it was invoked.
		field.Int64("test_attempt_duration").Optional(),
	}
}

// Edges of TestResult.
func (TestResultBES) Edges() []ent.Edge {
	return []ent.Edge{
		// Edge back to the test collection.
		edge.From("test_collection", TestCollection.Type).
			Ref("test_results").
			Unique(),

		// Files (logs, test.xml, undeclared outputs, etc) generated by that test action.
		edge.To("test_action_output", TestFile.Type).
			Annotations(
				entsql.OnDelete(entsql.Cascade),
			),

		// Message providing optional meta data on the execution of the test action,
		// if available.
		edge.To("execution_info", ExectionInfo.Type).
			Unique().
			Annotations(
				entsql.OnDelete(entsql.Cascade),
			),
	}
}
