<?php

namespace Tests\Unit;

use App\Models\AdditionalActivityTranslation;
use App\Services\ActivityTranslationDiffService;
use PHPUnit\Framework\TestCase;

class ActivityTranslationDiffServiceTest extends TestCase
{
    public function test_it_returns_scalar_and_nested_json_changes_with_readable_labels(): void
    {
        $service = new ActivityTranslationDiffService();

        $franchiseTranslation = new AdditionalActivityTranslation([
            'title' => 'Junior Robotics',
            'status' => 'publish',
            'activity_details' => [
                'duration' => '90',
                'activity_description' => 'Old description',
                'workshop_details' => [
                    'discover_section' => [
                        'heading' => 'Old discover heading',
                    ],
                ],
            ],
            'source_global_version' => 6,
        ]);

        $globalTranslation = new AdditionalActivityTranslation([
            'title' => 'Junior Robotics Updated',
            'status' => 'publish',
            'activity_details' => [
                'duration' => '120',
                'activity_description' => 'New description',
                'workshop_details' => [
                    'discover_section' => [
                        'heading' => 'New discover heading',
                    ],
                ],
            ],
            'source_global_version' => 7,
        ]);

        $changes = $service->diff($franchiseTranslation, $globalTranslation);
        $byField = collect($changes)->keyBy('field');

        $this->assertArrayHasKey('title', $byField->all());
        $this->assertArrayHasKey('activity_details.duration', $byField->all());
        $this->assertArrayHasKey('activity_details.activity_description', $byField->all());
        $this->assertArrayHasKey('activity_details.workshop_details.discover_section.heading', $byField->all());

        $this->assertSame('Duration', $byField['activity_details.duration']['label']);
        $this->assertSame('90', (string) $byField['activity_details.duration']['old']);
        $this->assertSame('120', (string) $byField['activity_details.duration']['new']);
    }

    public function test_it_ignores_non_content_system_fields(): void
    {
        $service = new ActivityTranslationDiffService();

        $franchiseTranslation = new AdditionalActivityTranslation([
            'title' => 'Same title',
            'adopted_from_global' => true,
            'source_global_version' => 6,
            'version' => 1,
        ]);

        $globalTranslation = new AdditionalActivityTranslation([
            'title' => 'Same title',
            'adopted_from_global' => false,
            'source_global_version' => 7,
            'version' => 8,
        ]);

        $changes = $service->diff($franchiseTranslation, $globalTranslation);

        $this->assertSame([], $changes);
    }
}
