import 'package:oto/oto/core/execution_context.dart';

/// Metadata for a single build artifact declared by the pipeline and reported
/// to remote consumers. Holds the declaration only (name + path); the runner
/// does not upload the file content itself.
class BuildArtifact {
  final String name;
  final String path;

  const BuildArtifact({required this.name, required this.path});

  Map<String, dynamic> toJson() => {
        'name': name,
        'path': path,
      };
}

class BuildResult {
  final bool success;
  final int exitCode;
  final Object? error;
  final StackTrace? stackTrace;
  final List<StepEvent> stepEvents;
  final List<BuildArtifact> artifacts;

  const BuildResult.success({
    this.stepEvents = const [],
    this.artifacts = const [],
  })  : success = true,
        exitCode = 0,
        error = null,
        stackTrace = null;

  const BuildResult.failure(
    this.error,
    this.stackTrace, {
    this.exitCode = 10,
    this.stepEvents = const [],
    this.artifacts = const [],
  }) : success = false;

  String get message => success
      ? 'Build completed successfully.'
      : (error?.toString() ?? 'Build failed.');

  Map<String, dynamic> toJson() => {
        'schemaVersion': 1,
        'type': 'executionResult',
        'success': success,
        'exitCode': exitCode,
        'message': message,
        'error': success
            ? null
            : {
                'type': error.runtimeType.toString(),
                'message': message,
              },
        'stepEvents': stepEvents.map((e) => e.toJson()).toList(),
        'artifacts': artifacts.map((e) => e.toJson()).toList(),
      };
}


