<?php
declare(strict_types=1);

namespace Autogedal\FrontendI18n\Model;

class TranslationComparator
{
    /**
     * @param string[] $sources
     * @param array<string,string> $translations
     * @return string[]
     */
    public function missing(array $sources, array $translations): array
    {
        $missing = [];

        foreach ($sources as $source) {
            if (!array_key_exists($source, $translations)) {
                $missing[] = $source;
            }
        }

        return $missing;
    }

    /**
     * @param string[] $sources
     * @param array<string,string> $translations
     * @return array<int,array{source:string,translation:string}>
     */
    public function untranslated(array $sources, array $translations): array
    {
        $sourceLookup = array_fill_keys($sources, true);
        $rows = [];

        foreach ($translations as $source => $translation) {
            if (!isset($sourceLookup[$source])) {
                continue;
            }

            $normalizedSource = trim($source);
            $normalizedTranslation = trim($translation);
            if ($normalizedTranslation === '' || $normalizedTranslation === $normalizedSource) {
                $rows[] = [
                    'source' => $source,
                    'translation' => $translation,
                ];
            }
        }

        return $rows;
    }

    /**
     * @param array<int,array{source:string,translation:string,raw_source?:string,raw_translation?:string,line:int}> $rows
     * @return array<int,array{source:string,first_line:int,duplicate_line:int,first_translation:string,duplicate_translation:string,effective_translation:string,status:string}>
     */
    public function duplicates(array $rows): array
    {
        $seen = [];
        $duplicates = [];

        foreach ($rows as $row) {
            $source = array_key_exists('raw_source', $row) ? (string)$row['raw_source'] : (string)$row['source'];
            if (trim($source) === '') {
                continue;
            }

            $translation = array_key_exists('raw_translation', $row) ? (string)$row['raw_translation'] : (string)$row['translation'];
            $line = (int)$row['line'];

            if (!isset($seen[$source])) {
                $seen[$source] = [
                    'line' => $line,
                    'translation' => $translation,
                ];
                continue;
            }

            $first = $seen[$source];
            $duplicates[] = [
                'source' => $source,
                'first_line' => $first['line'],
                'duplicate_line' => $line,
                'first_translation' => $first['translation'],
                'duplicate_translation' => $translation,
                'effective_translation' => $translation,
                'status' => $translation === $first['translation'] ? 'duplicate-same' : 'duplicate-different',
            ];

            $seen[$source] = [
                'line' => $line,
                'translation' => $translation,
            ];
        }

        return $duplicates;
    }
}
