// OpenSTA, Static Timing Analyzer
// Copyright (c) 2026, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// This notice may not be removed or altered from any source distribution.

#include "ReportParasiticAnnotation.hh"

#include "ArcDelayCalc.hh"
#include "ContainerHelpers.hh"
#include "Graph.hh"
#include "Network.hh"
#include "NetworkCmp.hh"
#include "Parasitics.hh"
#include "PortDirection.hh"
#include "Report.hh"
#include "Scene.hh"

namespace sta {

class ReportParasiticAnnotation : public StaState
{
public:
  ReportParasiticAnnotation(Parasitics *parasitics,
                            bool report_unannotated,
                            const Scene *scene,
                            StaState *sta);
  void reportAnnotation();

private:
  void reportAnnotationCounts();
  void findCounts();
  void findCounts(Instance *inst);
  void findCounts(Net *net);

  Parasitics *parasitics_;
  bool report_unannotated_;
  const Scene *scene_;
  const MinMax *min_max_;
  PinSeq unannotated_;
  PinSeq partially_annotated_;
};

void
reportParasiticAnnotation(Parasitics *parasitics,
                          bool report_unannotated,
                          const Scene *scene,
                          StaState *sta)
{
  ReportParasiticAnnotation report_annotation(parasitics, report_unannotated, scene,
                                              sta);
  report_annotation.reportAnnotation();
}

ReportParasiticAnnotation::ReportParasiticAnnotation(Parasitics *parasitics,
                                                     bool report_unannotated,
                                                     const Scene *scene,
                                                     StaState *sta) :
  StaState(sta),
  parasitics_(parasitics),
  report_unannotated_(report_unannotated),
  scene_(scene),
  min_max_(MinMax::max())
{
}

void
ReportParasiticAnnotation::reportAnnotation()
{
  findCounts();
  reportAnnotationCounts();
}

void
ReportParasiticAnnotation::reportAnnotationCounts()
{
  report_->report("Found {} unannotated drivers.", unannotated_.size());
  if (report_unannotated_) {
    sort(unannotated_, PinPathNameLess(network_));
    for (const Pin *drvr_pin : unannotated_)
      report_->report(" {}", network_->pathName(drvr_pin));
  }

  report_->report("Found {} partially unannotated drivers.",
                  partially_annotated_.size());
  if (report_unannotated_) {
    sort(partially_annotated_, PinPathNameLess(network_));
    for (const Pin *drvr_pin : partially_annotated_) {
      report_->report(" {}", network_->pathName(drvr_pin));

      Parasitic *parasitic = parasitics_->findParasiticNetwork(drvr_pin);
      if (parasitic) {
        PinSet unannotated_loads =
            parasitics_->unannotatedLoads(parasitic, drvr_pin);
        for (const Pin *load_pin : unannotated_loads)
          report_->report("  {}", network_->pathName(load_pin));
      }
    }
  }
}

void
ReportParasiticAnnotation::findCounts()
{
  VertexIterator vertex_iter(graph_);
  while (vertex_iter.hasNext()) {
    Vertex *vertex = vertex_iter.next();
    Pin *pin = vertex->pin();
    PortDirection *dir = network_->direction(pin);
    if (vertex->isDriver(network_) && !dir->isInternal()) {
      Parasitic *parasitic = parasitics_->findParasiticNetwork(pin);
      if (parasitic == nullptr)
        parasitic =
            arc_delay_calc_->findParasitic(pin, RiseFall::rise(), scene_, min_max_);
      if (parasitic) {
        PinSet unannotated_loads = parasitics_->unannotatedLoads(parasitic, pin);
        if (!unannotated_loads.empty())
          partially_annotated_.push_back(pin);
      }
      else
        unannotated_.push_back(pin);
    }
  }
}

}  // namespace sta
