<?php

declare(strict_types=1);

use Builder\Core\Foundation\Revisions\RevisionDataExtractor;
use Builder\Core\Foundation\Revisions\RevisionDiff;

it('extracts media ids from direct media fields', function (): void {
    $data = [
        RevisionDiff::KEY_MEDIA => [
            'hero_image' => [1, 2],
            'gallery' => [3],
        ],
    ];

    expect(RevisionDataExtractor::mediaIds($data))->toBe([1, 2, 3]);
});

it('extracts media ids from block media fields', function (): void {
    $data = [
        RevisionDiff::KEY_BLOCKS => [
            'content' => [
                ['key' => 'image_block', 'media' => ['image' => [10, 11]]],
                ['key' => 'text_block'],
            ],
        ],
    ];

    expect(RevisionDataExtractor::mediaIds($data))->toBe([10, 11]);
});

it('extracts media ids from translated block media fields', function (): void {
    $data = [
        RevisionDiff::KEY_TRANSLATIONS => [
            'en' => [
                RevisionDiff::KEY_BLOCKS => [
                    'content' => [
                        ['key' => 'image_block', 'media' => ['image' => [20]]],
                    ],
                ],
            ],
        ],
    ];

    expect(RevisionDataExtractor::mediaIds($data))->toBe([20]);
});

it('returns empty array when no media ids present', function (): void {
    expect(RevisionDataExtractor::mediaIds([]))->toBe([]);
    expect(RevisionDataExtractor::mediaIds([RevisionDiff::KEY_BLOCKS => []]))->toBe([]);
});

it('extracts block keys from direct blocks', function (): void {
    $data = [
        RevisionDiff::KEY_BLOCKS => [
            'content' => [
                ['key' => 'heading_block', 'text' => 'Hello'],
                ['key' => 'image_block'],
            ],
        ],
    ];

    expect(RevisionDataExtractor::blockKeys($data))->toBe(['heading_block', 'image_block']);
});

it('extracts block keys from translated blocks', function (): void {
    $data = [
        RevisionDiff::KEY_TRANSLATIONS => [
            'en' => [
                RevisionDiff::KEY_BLOCKS => [
                    'content' => [
                        ['key' => 'text_block'],
                    ],
                ],
            ],
            'nl' => [
                RevisionDiff::KEY_BLOCKS => [
                    'content' => [
                        ['key' => 'text_block'],
                        ['key' => 'cta_block'],
                    ],
                ],
            ],
        ],
    ];

    expect(RevisionDataExtractor::blockKeys($data))->toBe(['text_block', 'text_block', 'cta_block']);
});

it('skips blocks without a key', function (): void {
    $data = [
        RevisionDiff::KEY_BLOCKS => [
            'content' => [
                ['text' => 'No key here'],
                ['key' => 'valid_block'],
            ],
        ],
    ];

    expect(RevisionDataExtractor::blockKeys($data))->toBe(['valid_block']);
});

it('returns empty array when no blocks present', function (): void {
    expect(RevisionDataExtractor::blockKeys([]))->toBe([]);
});
