use crate::config::parameters::*;

pub fn lightcurve_amplitude(axis_ratio_a_b: f64) -> f64 {
    2.5 * axis_ratio_a_b.log10()
}

pub fn rotation_period_from_lightcurve(frequency_hz: f64) -> f64 {
    if frequency_hz < 1.0e-30 {
        return f64::INFINITY;
    }
    0.5 / frequency_hz
}

pub fn spin_barrier_period(density: f64) -> f64 {
    (3.0 * PI / (G * density)).sqrt()
}

pub fn tumbling_damping_timescale(
    radius: f64,
    density: f64,
    rotation_period: f64,
    rigidity: f64,
) -> f64 {
    let mu = rigidity;
    let r_km = radius / 1000.0;
    let p_hr = rotation_period / 3600.0;
    let k3 = 17.0;
    mu / (density * k3) * p_hr.powi(3) / (r_km * r_km) * 1.0e9 * YEAR
}

pub fn binary_eclipse_depth(
    radius_primary: f64,
    radius_secondary: f64,
    albedo_primary: f64,
    albedo_secondary: f64,
) -> f64 {
    let area_p = PI * radius_primary * radius_primary * albedo_primary;
    let area_s = PI * radius_secondary * radius_secondary * albedo_secondary;
    let total = area_p + area_s;
    if total < 1.0e-30 {
        return 0.0;
    }
    let smaller = area_p.min(area_s);
    smaller / total
}

pub fn phase_angle_from_geometry(
    distance_sun_body: f64,
    distance_body_obs: f64,
    distance_sun_obs: f64,
) -> f64 {
    let cos_phase = (distance_sun_body * distance_sun_body + distance_body_obs * distance_body_obs
        - distance_sun_obs * distance_sun_obs)
        / (2.0 * distance_sun_body * distance_body_obs);
    cos_phase.clamp(-1.0, 1.0).acos()
}
