with open(r'D:\Dev\Projects\pdf-umbenenner-parent\pdf-umbenenner-adapter-in-gui\src\main\java\de\gecheckt\pdf\umbenenner\adapter\in\gui\batchrun\GuiBatchRunTab.java', 'rb') as f:
    content = f.read()

normalized = content.replace(b'\r\n', b'\n')

old = (
    b'    private static String buildDetailText(GuiBatchRunResultRow row) {\n'
    b'        StringBuilder builder = new StringBuilder();\n'
    b'        builder.append("Originaldateiname: ").append(row.originalFileName()).append(\'\\n\');\n'
    b'        if (row.resetPending()) {\n'
    b'            builder.append(\'\\n\').append(GuiBatchRunResultRow.RESET_PENDING_LABEL);\n'
    b'            return builder.toString();\n'
    b'        }\n'
    b'        row.effectiveFileName()\n'
    b'                .ifPresent(name -> builder.append("Neuer Dateiname: ").append(name).append(\'\\n\'));\n'
    b'        row.resolvedDate()\n'
    b'                .ifPresent(date -> builder.append("Datum: ")\n'
    b'                        .append(DateTimeFormatter.ISO_LOCAL_DATE.format(date)).append(\'\\n\'));\n'
    b'        builder.append(\'\\n\');\n'
    b'        row.aiReasoning().ifPresentOrElse(\n'
    b'                reasoning -> builder.append(reasoning),\n'
    b'                () -> {\n'
    b'                    row.aiFailureMessage().ifPresent(msg ->\n'
    b'                            builder.append("\\u26A0 Fehler: ")\n'
    b'                                    .append(AiFailureMessageTranslator.translate(msg))\n'
    b'                                    .append("\\n\\n"));\n'
    b'                    builder.append(NO_REASONING_TEXT);\n'
    b'                });\n'
    b'        return builder.toString();\n'
    b'    }'
)

new = (
    b'    private static final DateTimeFormatter DETAIL_DATE_FORMAT =\n'
    b'            DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm", Locale.GERMANY);\n'
    b'\n'
    b'    private static String buildDetailText(GuiBatchRunResultRow row) {\n'
    b'        StringBuilder builder = new StringBuilder();\n'
    b'        builder.append("Originaldateiname: ").append(row.originalFileName()).append(\'\\n\');\n'
    b'        if (row.resetPending()) {\n'
    b'            builder.append(\'\\n\').append(GuiBatchRunResultRow.RESET_PENDING_LABEL);\n'
    b'            return builder.toString();\n'
    b'        }\n'
    b'        if (row.status() == DocumentCompletionStatus.SKIPPED_ALREADY_PROCESSED) {\n'
    b'            builder.append(\'\\n\');\n'
    b'            row.historicalContext().ifPresentOrElse(ctx -> {\n'
    b'                ctx.lastSuccessInstant().ifPresentOrElse(\n'
    b'                        instant -> builder.append("Bereits erfolgreich verarbeitet am ")\n'
    b'                                .append(DETAIL_DATE_FORMAT.format(\n'
    b'                                        instant.atZone(ZoneId.systemDefault())))\n'
    b'                                .append(\'.\'),\n'
    b'                        () -> builder.append("Bereits erfolgreich verarbeitet."));\n'
    b'                ctx.lastTargetFileName().ifPresent(name ->\n'
    b'                        builder.append(\'\\n\').append("Zieldatei: ").append(name).append(\'.\'));\n'
    b'            }, () -> builder.append("Bereits erfolgreich verarbeitet."));\n'
    b'            return builder.toString();\n'
    b'        }\n'
    b'        if (row.status() == DocumentCompletionStatus.SKIPPED_FINAL_FAILURE) {\n'
    b'            builder.append(\'\\n\');\n'
    b'            row.historicalContext().ifPresentOrElse(ctx ->\n'
    b'                    ctx.lastFailureInstant().ifPresentOrElse(\n'
    b'                            instant -> builder.append("Endg\\u00fcltig fehlgeschlagen am ")\n'
    b'                                    .append(DETAIL_DATE_FORMAT.format(\n'
    b'                                            instant.atZone(ZoneId.systemDefault())))\n'
    b'                                    .append(". Erneute Verarbeitung nur nach Reset m\\u00f6glich."),\n'
    b'                            () -> builder.append(\n'
    b'                                    "Endg\\u00fcltig fehlgeschlagen. Erneute Verarbeitung nur nach Reset m\\u00f6glich.")),\n'
    b'                    () -> builder.append(\n'
    b'                            "Endg\\u00fcltig fehlgeschlagen. Erneute Verarbeitung nur nach Reset m\\u00f6glich."));\n'
    b'            return builder.toString();\n'
    b'        }\n'
    b'        row.effectiveFileName()\n'
    b'                .ifPresent(name -> builder.append("Neuer Dateiname: ").append(name).append(\'\\n\'));\n'
    b'        row.resolvedDate()\n'
    b'                .ifPresent(date -> builder.append("Datum: ")\n'
    b'                        .append(DateTimeFormatter.ISO_LOCAL_DATE.format(date)).append(\'\\n\'));\n'
    b'        builder.append(\'\\n\');\n'
    b'        row.aiReasoning().ifPresentOrElse(\n'
    b'                reasoning -> builder.append(reasoning),\n'
    b'                () -> {\n'
    b'                    row.aiFailureMessage().ifPresent(msg ->\n'
    b'                            builder.append("\\u26A0 Fehler: ")\n'
    b'                                    .append(AiFailureMessageTranslator.translate(msg))\n'
    b'                                    .append("\\n\\n"));\n'
    b'                    builder.append(NO_REASONING_TEXT);\n'
    b'                });\n'
    b'        return builder.toString();\n'
    b'    }'
)

if old in normalized:
    result = normalized.replace(old, new)
    result = result.replace(b'\n', b'\r\n')
    with open(r'D:\Dev\Projects\pdf-umbenenner-parent\pdf-umbenenner-adapter-in-gui\src\main\java\de\gecheckt\pdf\umbenenner\adapter\in\gui\batchrun\GuiBatchRunTab.java', 'wb') as f:
        f.write(result)
    print('SUCCESS')
else:
    print('NOT FOUND')
    for i, line in enumerate(old.split(b'\n')):
        if line and line not in normalized:
            print(f'Missing line {i}: {repr(line)}')
