<?php

namespace App\Services\TranslationDiff;

/**
 * Filters translation / preview diff rows so "Update available" reflects
 * meaningful content only (not status, sync metadata, etc.).
 */
class MeaningfulContentDiffService
{
    /** Field path segments (leaf or any segment) treated as non-content for update UX. */
    private const IGNORED_PATH_SEGMENTS = [
        'status',
        'is_enabled',
        'is_global',
        'franchisee_id',
        'parent_id',
        'parent_program_id',
        'parent_activity_id',
        'parent_form_id',
        'parent_page_id',
        'parent_testimonials_id',
        'version',
        'source_global_version',
        'source_global_translation_id',
        'source_global_page_id',
        'source_language_code',
        'adopted_from_global',
        'last_local_edit_at',
        'created_at',
        'updated_at',
        'deleted_at',
        'approved_at',
        'approval_status',
        'pending_approval',
        'submitted_for_approval_at',
        'language',
        'language_code',
        'order_no',
        'is_required_for_translation',
        'is_global_suggestion',
    ];

    /**
     * @param  array<int, array<string, mixed>>  $changes
     * @return array<int, array<string, mixed>>
     */
    public function filter(array $changes): array
    {
        return array_values(array_filter($changes, fn(array $row) => !$this->isIgnoredDiffRow($row)));
    }

    /**
     * @param  array<int, array<string, mixed>>  $changes
     */
    public function hasMeaningfulChanges(array $changes): bool
    {
        return count($this->filter($changes)) > 0;
    }

    /**
     * @param  array<string, mixed>  $row
     */
    private function isIgnoredDiffRow(array $row): bool
    {
        $field = (string) ($row['field'] ?? '');
        $label = (string) ($row['label'] ?? $field);

        if ($this->isIgnoredFormSummaryLabel($field) || $this->isIgnoredFormSummaryLabel($label)) {
            return true;
        }

        if ($field === '') {
            return false;
        }

        return $this->isIgnoredFieldPath($field);
    }

    private function isIgnoredFormSummaryLabel(string $text): bool
    {
        $t = trim($text);
        if ($t === '') {
            return false;
        }

        if (preg_match('/^Form Details.*\bStatus\b/i', $t)) {
            return true;
        }

        if (preg_match('/^Notifications.*\bstatus\b changed/i', $t)) {
            return true;
        }

        return (bool) preg_match('/\bFORM DETAILS\b.*\bSTATUS\b/i', $t);
    }

    private function isIgnoredFieldPath(string $field): bool
    {
        $normalized = preg_replace('/\[\d+\]/', '', $field) ?? $field;
        $parts = preg_split('/[.\[\]]+/', $normalized, -1, PREG_SPLIT_NO_EMPTY);
        if (!is_array($parts)) {
            return false;
        }

        foreach ($parts as $part) {
            $leaf = strtolower((string) $part);
            if (in_array($leaf, self::IGNORED_PATH_SEGMENTS, true)) {
                return true;
            }
        }

        return false;
    }
}
