<?php

use App\Services\EmailTemplateRenderer;

it('renders activity placeholders', function () {
    /** @var EmailTemplateRenderer $renderer */
    $renderer = app(EmailTemplateRenderer::class);

    $activity = (object) [
        'note' => 'Call client about payment',
        'due_date' => '2025-10-15',
        'amount' => 123.45,
        'action' => (object) ['name' => 'CALL - OUTBOUND'],
        'decision' => (object) ['name' => 'Promise'],
    ];

    $tpl = [
        'subject' => 'Action: {{ activity.action.name }}',
        'html' => '<p>{{ activity.note }}</p><p>{{ activity.decision.name }} {{ activity.due_date }} {{ activity.amount }}</p>',
        'text' => 'Note: {{ activity.note }}',
    ];

    $out = $renderer->render($tpl, [
        'activity' => $activity,
    ]);

    expect($out['subject'])->toBe('Action: CALL - OUTBOUND');
    expect($out['html'])
        ->toContain('<p>Call client about payment</p>')
        ->toContain('<p>Promise 15.10.2025 123,45 €</p>');
    expect($out['text'])->toBe('Note: Call client about payment');
});
it('renders placeholders in subject, html and text', function () {
    $renderer = new EmailTemplateRenderer;

    $template = [
        'subject' => 'Hello {{ person.full_name }} - {{ contract.reference }}',
        'html' => '<p>Case: {{ case.uuid }}</p><p>Meta: {{ contract.meta.foo }}</p><p>Amount: {{ contract.amount }}</p>',
        'text' => 'Client: {{ client.uuid }} Extra: {{ extra.note }}',
    ];

    $ctx = [
        'person' => (object) ['first_name' => 'Jane', 'last_name' => 'Doe', 'email' => 'jane@example.test'],
        'client' => (object) ['uuid' => 'cl-123'],
        'client_case' => (object) ['uuid' => 'cc-456', 'reference' => 'REF-1'],
        'contract' => (object) ['uuid' => 'co-789', 'reference' => 'CON-42', 'amount' => 1000, 'meta' => ['foo' => 'bar']],
        'extra' => ['note' => 'hello'],
    ];

    $result = $renderer->render($template, $ctx);

    expect($result['subject'])->toBe('Hello Jane Doe - CON-42');
    expect($result['html'])->toContain('Case: cc-456');
    expect($result['html'])->toContain('Meta: bar');
    expect($result['html'])->toContain('Amount: 1.000,00 €');
    expect($result['text'])->toBe('Client: cl-123 Extra: hello');
});
