// Regenerates test/golden/* after intentional template changes.
// Run from generator/: dart run tool/refresh_goldens.dart

import 'dart:io';

import 'package:path/path.dart' as p;

import '../src/models.dart';
import '../src/renderer.dart';
import '../src/resolver.dart';
import '../test/golden_briefs.dart';

Future<void> main() async {
  final templateSrc = _templateDir();
  if (!Directory(templateSrc).existsSync()) {
    stderr.writeln('Template dir not found: $templateSrc');
    exitCode = 1;
    return;
  }

  final cases = <String, ProjectBrief>{
    'bloc-clean-firebase': kBlocCleanFirebaseBrief,
    'riverpod-ff-supabase': kRiverpodFfSupabaseBrief,
    'getx-mvc-rest': kGetxMvcRestBrief,
  };

  for (final e in cases.entries) {
    final profile = e.key;
    final brief = e.value;
    final rendered = await Renderer.render(
      brief: brief,
      templateFiles: Resolver.resolve(brief),
      templateSrc: templateSrc,
    );
    for (final out in rendered.entries) {
      final file = File('test/golden/$profile/${out.key}');
      await file.parent.create(recursive: true);
      await file.writeAsString(out.value);
      stdout.writeln('Wrote ${file.path}');
    }
  }
}

String _templateDir() {
  final scriptDir = File(Platform.script.toFilePath()).parent;
  return p.normalize(p.join(scriptDir.path, '..', '..', 'templates'));
}
