"""Build a small synthetic gzipped save XML for exercising the pipeline
end-to-end without a real X4 save file.

Updated 2026-09-20 to match the structure confirmed against a real save
(save_005.xml.gz): <player money="..."/> nests under <info>, not directly
under <savegame>; <factions> nests under <universe>; money is stored as an
integer in hundredths of a credit; blueprints hang off the player's own
<component class="player"> in the universe tree, not off the top-level
<player> tag; and each faction's <relations> block has one row per *other*
faction, not just one for the player -- this sample includes an extra
non-player row per faction so that filtering logic actually gets
exercised instead of trivially passing.
"""

from __future__ import annotations

import gzip
import os

SAMPLE_XML = """<?xml version="1.0" encoding="utf-8"?>
<savegame version="9">
  <info save="quicksave" date="2026-09-19" playtime="123456">
    <player name="Boso Tender" location="{20004,140011}" money="750000000"/>
  </info>
  <universe>
    <factions>
      <faction id="argon">
        <relations>
          <relation faction="teladi" relation="0.10"/>
          <relation faction="player" relation="0.35"/>
        </relations>
      </faction>
      <faction id="teladi">
        <relations>
          <relation faction="player" relation="-0.10"/>
          <relation faction="argon" relation="0.10"/>
        </relations>
      </faction>
      <faction id="visitor042">
        <relations>
          <relation faction="player" relation="-1.00"/>
        </relations>
      </faction>
    </factions>
    <component class="galaxy" id="galaxy_1" name="X4 Universe">
      <connections>
        <connection id="c1">
          <component class="sector" id="sec_argon_prime" name="Argon Prime">
            <connections>
              <connection id="c1a">
                <component class="zone" id="zone_1" name="Zone A">
                  <connections>
                    <connection id="c1a0">
                      <component class="player" id="char_1" name="Boso Tender"
                                  macro="character_player_macro" owner="player">
                        <blueprints>
                          <blueprint ware="station_module_prod_claytronics_macro"/>
                          <blueprint ware="station_module_dockarea_macro"/>
                        </blueprints>
                      </component>
                    </connection>
                    <connection id="c1a1">
                      <component class="station" id="stat_1" name="Player Trade Station"
                                  macro="station_hq_macro" owner="player">
                        <wares>
                          <ware ware="energycells" amount="4200"/>
                          <ware ware="silicon" amount="1800"/>
                        </wares>
                      </component>
                    </connection>
                    <connection id="c1a2">
                      <component class="station" id="stat_2" name="New Outpost"
                                  macro="station_small_macro" owner="player">
                        <wares/>
                      </component>
                    </connection>
                    <connection id="c1a3">
                      <component class="ship_l" id="ship_1" name="Trusty Hauler"
                                  macro="ship_l_trader_macro" owner="player"/>
                    </connection>
                    <connection id="c1a4">
                      <component class="ship_s" id="ship_2" name="Scout One"
                                  macro="ship_s_scout_macro" owner="player"/>
                    </connection>
                    <connection id="c1a5">
                      <component class="station" id="stat_3" name="Pirate Base"
                                  macro="station_pirate_macro" owner="buccaneers">
                        <wares>
                          <ware ware="weapons" amount="20"/>
                        </wares>
                      </component>
                    </connection>
                  </connections>
                </component>
              </connection>
            </connections>
          </component>
        </connection>
        <connection id="c2">
          <component class="sector" id="sec_second" name="Second Contact">
            <connections/>
          </component>
        </connection>
      </connections>
    </component>
  </universe>
  <log>
    <entry text="Docked at Player Trade Station"/>
  </log>
</savegame>
"""

# A second, sparse scenario: early game, low cash, no stations/ships/
# blueprints yet -- exercises the "not found" / low-cash branches.
SAMPLE_XML_POOR = """<?xml version="1.0" encoding="utf-8"?>
<savegame version="9">
  <info save="quicksave" date="2026-09-19" playtime="1000">
    <player name="New Guy" location="{20004,140011}" money="1200000"/>
  </info>
  <universe>
    <factions/>
    <component class="galaxy" id="g1" name="Universe">
      <connections>
        <connection id="c1">
          <component class="sector" id="s1" name="Home Sector">
            <connections/>
          </component>
        </connection>
      </connections>
    </component>
  </universe>
</savegame>
"""


def _write(path: str, xml: str) -> str:
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with gzip.open(path, "wt", encoding="utf-8") as f:
        f.write(xml)
    return path


def write_sample(path: str) -> str:
    return _write(path, SAMPLE_XML)


def write_poor_sample(path: str) -> str:
    return _write(path, SAMPLE_XML_POOR)


if __name__ == "__main__":
    fixtures_dir = os.path.join(os.path.dirname(__file__), "fixtures")
    out1 = write_sample(os.path.join(fixtures_dir, "sample_save.xml.gz"))
    print(f"Wrote {out1}")
    out2 = write_poor_sample(os.path.join(fixtures_dir, "sample_save_poor.xml.gz"))
    print(f"Wrote {out2}")
