import cadquery as cq
from cadquery import exporters

K = 1000.0  # scale factor: m -> mm

def build_flag_step(
    Ly: float = 0.10*K,   # extension in Y: 0 -> Ly (m)
    Lz: float = 0.12*K,   # total extension in Z: -Lz/2 -> +Lz/2 (m)
    out_name: str = "flag_surface.step",
) -> None:
    """
    Creates a rectangular surface in the ZY plane (x=0) with CadQuery:
      y in [0, Ly]
      z in [-Lz/2, +Lz/2]
    and exports a STEP file.
    """

    halfL = 0.5 * Lz

    wp = (
        cq.Workplane("YZ")
        .rect(Ly, Lz, centered=(False, True))
    )

    # rectangle wire
    wire = wp.val()          # first object in the stack: rectangle wire
    face = cq.Face.makeFromWires(wire)

    # export as STEP
    exporters.export(face, out_name)
    print(f"[GEOM] STEP written: {out_name}")

def build_fluid_flag_step(
    # flag
    Ly_flag: float = 0.10 * K,
    Lz_flag: float = 0.12 * K,
    # fluid domain
    Lx_dom: float = 0.5 * K,   
    Ly_dom: float = 1.5 * K,   
    Lz_dom: float = 0.5 * K,   
    out_name: str = "fluid_flag.step",
) -> None:
    """
    3D fluid domain + internal flag surface, same reference system:

      - flow direction along +Y
      - Z aligned with the flag span
    """
    # Fluid solid: rectangle in XY, extruded in Z
    enclosure = (
        cq.Workplane("XY")
        .center(0, -0.5 * K)
        .rect(Lx_dom, Ly_dom, centered=(True, False))  # X centered, Y: [0, Ly_dom]
        .extrude(Lz_dom/2, both=True)                  # domain from -Lz_dom/2 to +Lz_dom/2
    )
    enclosure_solid = enclosure.val()

    halfL = 0.5 * Lz_flag

    wp = (
        cq.Workplane("YZ")
        .rect(Ly_flag, Lz_flag, centered=(False, True))
    )

    # rectangle wire
    wire = wp.val()          # first object in the stack: rectangle wire
    flag_face = cq.Face.makeFromWires(wire)

    compound = cq.Compound.makeCompound([
        enclosure_solid,
        flag_face
    ])

    # Export a single STEP containing both bodies
    cq.exporters.export(compound, out_name)
    print(f"[GEOM] Fluid+flag STEP written: {out_name}")


if __name__ == "__main__":
    build_flag_step()
    build_fluid_flag_step()
