# Rebuild a representative, fully validated artifact set and its replay manifest.
from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path

from generate import generate
from lodge.plan import DEFAULT_SEED
from lodge.validation import geometry_signature

SAMPLES = [
    ('default', DEFAULT_SEED, {}),
    ('cliff_courtyard', 0, {}),
    ('winter_garden_crag', 2, {}),
    ('terrace_tower', 8, {}),
    ('three_wing_inn', 13, {}),
    ('cross_gabled_lodge', 21, {}),
    ('woodland_house', 31, {}),
    ('watchtower_inn', 37, {}),
    ('twin_garden_wings', 20260923, {}),
    ('compact_cottage', 907, {
        'topology': 'single',
        'width': 15,
        'depth': 17,
        'floors': 1,
        'ridge': 'z',
        'profile': 'chalet',
        'wing_floors': 1,
        'tower': 'none',
        'conservatory': False,
        'porch': 'full',
        'balcony': False,
        'dormers': 0,
        'terrain': 'garden',
    }),
    ('cross_roof_conservatory', 907, {
        'topology': 'ell',
        'width': 15,
        'depth': 17,
        'floors': 1,
        'ridge': 'x',
        'profile': 'chalet',
        'wing_size': 9,
        'wing_floors': 1,
        'tower': 'stone',
        'tower_width': 9,
        'conservatory': True,
        'porch': 'entry',
        'balcony': False,
        'dormers': 2,
        'terrain': 'terrace',
    }),
]


def main():
    """Export each sample and record parameters, semantic reload checks, and signatures."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--directory', type=Path, default=Path('samples'))
    args = parser.parse_args()
    args.directory.mkdir(parents=True, exist_ok=True)
    entries = []
    for name, seed, overrides in SAMPLES:
        output = args.directory / f'{name}.litematic'
        build = generate(seed, overrides, output)
        entry = {
            'name': name,
            'seed': seed,
            'overrides': overrides,
            'schematic': output.name,
            'resolved_record': output.with_suffix('.json').name,
            'parameters': build.plan.parameters,
            'size_xyz': build.plan.size_xyz,
            'validation': build.validation,
            'placed_geometry_signature': geometry_signature(build.scene),
            'sha256': hashlib.sha256(output.read_bytes()).hexdigest(),
        }
        entries.append(entry)
        print(f'{name}: seed {seed}, {build.validation["rooms"]} rooms, validated and reloaded', flush=True)
    manifest = {
        'minecraft_version': 'Java 1.21.1',
        'default_seed': DEFAULT_SEED,
        'command': 'python sample_batch.py',
        'samples': entries,
        'observed_samples': len(entries),
        'observed_geometry_signatures': len({e['placed_geometry_signature'] for e in entries}),
        'observed_topologies': sorted({e['parameters']['topology'] for e in entries}),
        'signature_method': 'Actual placed slate roof coordinates, floor support cells, and occupied room cells; normalized to the main volume. Material texture choices are collapsed.',
    }
    (args.directory / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')


if __name__ == '__main__':
    main()
