// src/FinSet.cpp

#include "FinSet.h"

// Constructor
FinSet::FinSet(const std::string& name,
               int numberOfFins,
               double rootChord,
               double tipChord,
               double span,
               double sweepLength,
               double thickness,
               double materialDensity)
    : name_(name),
      numberOfFins_(numberOfFins),
      rootChord_(rootChord),
      tipChord_(tipChord),
      span_(span),
      sweepLength_(sweepLength),
      thickness_(thickness),
      materialDensity_(materialDensity)
{}

// Public Methods

double FinSet::calculateNormalForceCoefficient() const {
    // Simplified thin airfoil theory estimation
    // Cn_alpha ? (number of fins) * (planform area) / (reference area)
    // Assume reference area (body) is much smaller for initial versions

    double planformArea = calculateSingleFinArea();
    double effectiveArea = numberOfFins_ * planformArea;

    // Return some typical small value scaled by fin count and area
    // (In reality, depends heavily on Mach number and sweep angle)
    return 2.0 * effectiveArea; // Very basic approximation
}

double FinSet::calculateCenterOfPressure() const {
    // Approximate aerodynamic center (x_cp) for trapezoidal fin
    // Formula from Barrowman equations (simplified for low-speed flight)
    double meanAerodynamicChord = (rootChord_ + tipChord_) / 2.0;

    double x_leading_edge = sweepLength_; // Assume sweepLength at root
    double x_cp = x_leading_edge + (meanAerodynamicChord / 2.0);

    return x_cp;
}

double FinSet::calculateDragArea() const {
    // Drag Area = Cd_fins * total projected area
    double Cd_fin = 0.01; // Approximate for small rocket fins
    double planformArea = calculateSingleFinArea();
    double totalArea = numberOfFins_ * planformArea;
    return Cd_fin * totalArea;
}

double FinSet::calculateMass() const {
    // Volume = area * thickness
    double singleArea = calculateSingleFinArea();
    double volume = singleArea * thickness_;
    double mass = numberOfFins_ * volume * materialDensity_;
    return mass;
}

const std::string& FinSet::getName() const {
    return name_;
}

// Private Methods

double FinSet::calculateSingleFinArea() const {
    // Area of a trapezoid: A = 0.5 * (root + tip) * span
    return 0.5 * (rootChord_ + tipChord_) * span_;
}
