"""
base_class_one_phase.py

Shared flow-property computation for single-phase tube correlations.
"""

import numpy as np
import fluids.friction as frict
from abc import ABC, abstractmethod


class OnePhaseTubeBase(ABC):
    """
    Computes Re, Pr, u, dh and the Darcy friction factor for internal
    single-phase tube flow.

    Parameters
    ----------
    fluid      : carbatpy Fluid object
    fluidstate : FluidState – single-phase state
    mdot       : float  – mass flow rate [kg/s]
    l          : float  – tube length [m]  (entry-length reference)
    D          : float  – inner diameter [m]
    d_small    : float, optional  – inner diameter if annulus [m], default 0
    """

    @abstractmethod
    def calc(self):
        """Return a dict with computed results."""

    eps_wall = 0.045e-3  # [m] – default wall roughness (commercial steel)

    def __init__(self, fluid, fluidstate, mdot, l, D, d_small=0):
        self.fluid      = fluid
        self.fluidstate = fluidstate
        self.mdot       = mdot
        self.l          = l
        self.D          = D
        self.d_small    = d_small

        self._compute_flow_props()

    def _compute_flow_props(self):
        fs = self.fluidstate
        self.dh = self.D - self.d_small

        Vdot    = self.mdot * fs.sp_volume
        self.u  = Vdot / (((self.D / 2) ** 2 - (self.d_small / 2) ** 2) * np.pi)

        self.Re = self.u * self.dh / (fs.viscosity * fs.sp_volume)  # B1-(10)  u * d / (eta * v)
        self.Pr = fs.viscosity * fs.cp / fs.thermal_conductivity

        if self.Re > 2300:
            self.f_s = frict.Colebrook(self.Re, self.eps_wall / self.dh)
        else:
            self.f_s = 64 / self.Re  # Hagen-Poiseuille