# Build inpaint masks for the edit-routing test + a color-coded overlay to
# verify region placement before running the chain.
# White = inpaint (regenerate), black = preserve.
from PIL import Image, ImageDraw

W, H = 1408, 768
SEED = 'scripts/nuit-d5bd28cc.png'

# Regions on the seed (composition is preserved by inpaint, so seed coords hold
# across steps). Tune here after eyeballing scripts/overlay-all.png.
REGIONS = {
    'roof':      [(490, 300), (980, 300), (930, 140), (530, 140)],
    # Full boardwalk sweep — deck at the cabin down to the bottom-right exit.
    'path':      [(600, 520), (770, 500), (1015, 500), (1110, 560), (1408, 700),
                  (1408, 768), (560, 768)],
    'gable_end': [(500, 470), (665, 470), (665, 300), (635, 185), (520, 195), (500, 310)],
    'chimney':   [(840, 175), (895, 175), (895, 265), (840, 265)],
    # Exterior sconces on the entrance frame posts (either side of the door).
    'sconce_l':  [(842, 380), (866, 380), (866, 440), (842, 440)],
    'sconce_r':  [(905, 380), (929, 380), (929, 440), (905, 440)],
    # Entrance door in the glass front.
    'door':      [(852, 355), (915, 355), (915, 470), (852, 470)],
    # Front-right deck + the space above it (for a pergola/canopy).
    'deck':      [(875, 385), (1010, 385), (1010, 498), (875, 498)],
    # The plinth / stilts under the cabin box.
    'base':      [(495, 468), (965, 468), (965, 515), (495, 515)],
}

def mask_from(names):
    m = Image.new('L', (W, H), 0)
    d = ImageDraw.Draw(m)
    for n in names:
        d.polygon(REGIONS[n], fill=255)
    return m

# Individual + union masks used by the harness
masks = {
    'roof': ['roof'],
    'path': ['path'],
    'gable_end': ['gable_end'],
    'chimney': ['chimney'],
    'sconces': ['sconce_l', 'sconce_r'],
    'door': ['door'],
    'deck': ['deck'],
    'base': ['base'],
}
for name, names in masks.items():
    mask_from(names).save(f'scripts/mask-{name}.png')

# Color-coded overlay on the seed for a single visual sanity check
COLORS = {
    'roof': (255, 60, 60), 'path': (60, 120, 255), 'gable_end': (60, 220, 120),
    'chimney': (255, 160, 40), 'sconce_l': (230, 60, 230), 'sconce_r': (230, 60, 230),
    'door': (255, 240, 60), 'deck': (60, 230, 230), 'base': (180, 120, 60),
}
seed = Image.open(SEED).convert('RGB')
overlay = seed.copy()
tint = Image.new('RGB', (W, H))
td = ImageDraw.Draw(tint)
mask_all = Image.new('L', (W, H), 0)
md = ImageDraw.Draw(mask_all)
for n, poly in REGIONS.items():
    td.polygon(poly, fill=COLORS[n])
    md.polygon(poly, fill=110)  # blend strength
overlay = Image.composite(tint, seed, mask_all)
overlay.save('scripts/overlay-all.png')
print('Masks + scripts/overlay-all.png written.')
