import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:toml/toml.dart';

const Set<String> _placeholderValues = {
  'Action',
  'Action Label',
  'Approve Error',
  'Approve Success',
  'Body',
  'Code Hint',
  'Code Label',
  'Confirm',
  'Confirm Button',
  'Description',
  'Error',
  'Heading',
  'Input Label',
  'Invalid',
  'Label',
  'Load Failed',
  'Page Title',
  'Request Code',
  'Result Failure',
  'Sent',
  'Subtitle',
  'Title',
  'Update Success',
};

String? _readTomlValue(Map<String, dynamic> root, String key) {
  dynamic cursor = root;
  for (final part in key.split('.')) {
    if (cursor is! Map<String, dynamic>) {
      return null;
    }
    cursor = cursor[part];
  }
  return cursor is String ? cursor : null;
}

void main() {
  test('critical english copy does not expose placeholder values', () {
    final file = File('assets/translations/en.toml');
    final document = TomlDocument.parse(file.readAsStringSync());
    final translations = document.toMap();

    const criticalKeys = <String>[
      'ui.userfront.forgot.heading',
      'ui.userfront.forgot.input_label',
      'ui.userfront.forgot.title',
      'msg.userfront.forgot.description',
      'msg.userfront.forgot.sent',
      'ui.userfront.login.link.action_label',
      'ui.userfront.login.link.page_title',
      'ui.userfront.login.link.title',
      'ui.userfront.login.unregistered.action',
      'ui.userfront.login.unregistered.title',
      'ui.userfront.login.verification.action_label',
      'ui.userfront.login.verification.page_title',
      'ui.userfront.login.verification.title',
      'msg.userfront.login.qr.load_failed',
      'msg.userfront.login.short_code.invalid',
      'msg.userfront.login.unregistered.body',
      'ui.userfront.login_success.title',
      'msg.userfront.login_success.subtitle',
      'msg.userfront.profile.load_failed',
      'msg.userfront.profile.update_success',
      'ui.userfront.profile.password.title',
      'ui.userfront.profile.phone.code_hint',
      'msg.userfront.reset.invalid_body',
      'msg.userfront.reset.invalid_title',
      'ui.userfront.reset.subtitle',
      'ui.userfront.reset.title',
      'ui.userfront.signup.title',
      'msg.userfront.signup.agreement.title',
      'msg.userfront.signup.auth.title',
      'ui.userfront.signup.auth.email.label',
      'ui.userfront.signup.auth.email.title',
      'msg.userfront.signup.password.title',
      'msg.userfront.signup.profile.title',
      'msg.userfront.signup.success.body',
      'msg.userfront.signup.success.title',
      'ui.userfront.signup.success.action',
    ];

    final failures = <String>[];
    for (final key in criticalKeys) {
      final value = _readTomlValue(translations, key);
      if (value == null || value.trim().isEmpty) {
        failures.add('$key is missing');
        continue;
      }
      if (_placeholderValues.contains(value.trim())) {
        failures.add('$key uses placeholder "$value"');
      }
    }

    expect(
      failures,
      isEmpty,
      reason: failures.isEmpty ? null : failures.join('\n'),
    );
  });
}
