#include "core/render_plan.hpp"
#include "core/build_info.hpp"
#include <sstream>
namespace smartcut::core {
RenderPlan RenderPlan::from_project(const Project& project) {
  RenderPlan plan; if (!project.valid()) return plan;
  for (const auto& source : project.sources()) plan.source_fingerprints_.push_back(source.source_uuid);
  for (const auto& segment : project.segments()) { std::size_t audio_stream = static_cast<std::size_t>(-1); int audio_num = 0; int audio_den = 0; for (const auto& source : project.sources()) if (source.source_uuid == segment.source_uuid) { audio_stream = source.audio_stream; audio_num = source.audio_time_base_num; audio_den = source.audio_time_base_den; } const bool audio_ready = audio_stream != static_cast<std::size_t>(-1) && audio_num > 0 && audio_den > 0; const auto audio_policy = audio_ready ? "copy-complete-source-audio-packets" : "blocked-missing-audio-metadata"; plan.operations_.push_back({"CopyVideoPackets", segment.source_uuid, segment.video_stream, audio_stream, segment.first_display_frame, segment.end_display_frame_exclusive, "validated kept segment; boundary safety classification pending", audio_policy, "video", audio_num, audio_den}); plan.operations_.push_back({"CopyAudioPackets", segment.source_uuid, segment.video_stream, audio_stream, segment.first_display_frame, segment.end_display_frame_exclusive, audio_ready ? "validated audio stream metadata" : "audio metadata required before packet selection", audio_policy, "audio", audio_num, audio_den}); }
  return plan;
}
std::string RenderPlan::to_json() const {
  std::ostringstream output; output << "{\"schema\":\"render-plan-v1\",\"dependency_versions\":{\"smartcut\":\"" << smartcut::build_info::application_version << "\",\"ffmpeg\":\"9.0\",\"x265\":\"pinned-m1\"},\"source_fingerprints\":[";
  for (std::size_t i = 0; i < source_fingerprints_.size(); ++i) { if (i) output << ','; output << "\"" << source_fingerprints_[i] << "\""; }
  output << "],\"operations\":[";
  for (std::size_t i = 0; i < operations_.size(); ++i) { if (i) output << ','; const auto& op = operations_[i]; output << "{\"kind\":\"" << op.kind << "\",\"source_uuid\":\"" << op.source_uuid << "\",\"video_stream\":" << op.video_stream << ",\"audio_stream\":" << op.audio_stream << ",\"first_frame\":" << op.first_frame << ",\"end_frame\":" << op.end_frame << ",\"reason\":\"" << op.reason << "\",\"audio_policy\":\"" << op.audio_policy << "\",\"stream_type\":\"" << op.stream_type << "\",\"audio_time_base_num\":" << op.audio_time_base_num << ",\"audio_time_base_den\":" << op.audio_time_base_den << "}"; }
  output << "]}"; return output.str();
}
std::string RenderPlan::explain_text() const {
  std::ostringstream output;
  output << "SmartCut64 render plan: " << operations_.size() << " operations\n";
  for (std::size_t i = 0; i < operations_.size(); ++i) {
    const auto& operation = operations_[i];
    output << (i + 1) << ". " << operation.kind << " " << operation.stream_type
           << " frames " << operation.first_frame << '-' << operation.end_frame
           << " from " << operation.source_uuid << ": " << operation.reason;
    if (!operation.audio_policy.empty()) output << " [audio: " << operation.audio_policy << ']';
    output << '\n';
  }
  return output.str();
}
}  // namespace smartcut::core
