#!/usr/bin/env python3
"""
Migration: add lora_weight_min / lora_weight_max to every entity JSON.

For each JSON file in the target directories we:
  - Read the existing lora_weight value (default 1.0 if missing)
  - Write lora_weight_min = lora_weight (if not already set)
  - Write lora_weight_max = lora_weight (if not already set)
  - Leave lora_weight in place (the resolver still uses it as a fallback)

Directories processed (skip data/checkpoints — no LoRA weight there):
  data/characters/
  data/clothing/
  data/actions/
  data/styles/
  data/scenes/
  data/detailers/
  data/looks/
"""

import glob
import json
import os
import sys

DIRS = [
    'data/characters',
    'data/clothing',
    'data/actions',
    'data/styles',
    'data/scenes',
    'data/detailers',
    'data/looks',
]

dry_run = '--dry-run' in sys.argv
updated = 0
skipped = 0
errors = 0

for directory in DIRS:
    pattern = os.path.join(directory, '*.json')
    for path in sorted(glob.glob(pattern)):
        try:
            with open(path, 'r', encoding='utf-8') as f:
                data = json.load(f)

            lora = data.get('lora')
            if not isinstance(lora, dict):
                skipped += 1
                continue

            weight = float(lora.get('lora_weight', 1.0))
            changed = False

            if 'lora_weight_min' not in lora:
                lora['lora_weight_min'] = weight
                changed = True
            if 'lora_weight_max' not in lora:
                lora['lora_weight_max'] = weight
                changed = True

            if changed:
                if not dry_run:
                    with open(path, 'w', encoding='utf-8') as f:
                        json.dump(data, f, indent=2, ensure_ascii=False)
                        f.write('\n')
                print(f"  [{'DRY' if dry_run else 'OK'}]  {path}  weight={weight}")
                updated += 1
            else:
                skipped += 1

        except Exception as e:
            print(f"  [ERR] {path}: {e}", file=sys.stderr)
            errors += 1

print(f"\nDone. updated={updated}  skipped={skipped}  errors={errors}")
if dry_run:
    print("(dry-run — no files were written)")
