#!perl
use Cassandane::Tiny;

sub test_email_set_create_deterministic_boundaries
    :needs_component_sieve
    ($self)
{
    my $jmap = $self->{jmap};

    # Fixed messageId and sentAt ensure the server doesn't generate
    # random values, keeping the RFC822 output fully deterministic.
    my %emailProps = (
        mailboxIds => { '$inbox' => JSON::true },
        from => [{ name => "Test", email => 'test@example.com' }],
        to => [{ name => "Dest", email => 'dest@example.com' }],
        subject => "Deterministic boundary test",
        messageId => ['test1234@example.com'],
        sentAt => '2024-01-01T00:00:00Z',
        bodyStructure => {
            type => 'multipart/alternative',
            subParts => [{
                type => 'text/plain',
                partId => 'text',
            }, {
                type => 'text/html',
                partId => 'html',
            }],
        },
        bodyValues => {
            text => { value => "Hello world" },
            html => { value => "<p>Hello world</p>" },
        },
    );

    xlog $self, "Create a multipart email with text and html bodies";
    my $res = $jmap->CallMethods([
        ['Email/set', { create => { "a" => {%emailProps} } }, "R1"],
    ]);
    my $blobId1 = $res->[0][1]{created}{a}{blobId};
    $self->assert_not_null($blobId1);

    xlog $self, "Delete the email";
    my $emailId1 = $res->[0][1]{created}{a}{id};
    $res = $jmap->CallMethods([
        ['Email/set', { destroy => [$emailId1] }, "R2"],
    ]);
    $self->assert_str_equals($emailId1, $res->[0][1]{destroyed}[0]);

    xlog $self, "Create the exact same email again - must produce same blobId";
    $res = $jmap->CallMethods([
        ['Email/set', { create => { "b" => {%emailProps} } }, "R3"],
    ]);
    my $blobId2 = $res->[0][1]{created}{b}{blobId};
    $self->assert_not_null($blobId2);

    xlog $self, "Same input must produce same blobId (identical RFC822 output)";
    $self->assert_str_equals($blobId1, $blobId2);

    xlog $self, "Create email with different body content";
    my %differentProps = %emailProps;
    $differentProps{bodyValues} = {
        text => { value => "Hello different world" },
        html => { value => "<p>Hello different world</p>" },
    };
    $res = $jmap->CallMethods([
        ['Email/set', { create => { "c" => {%differentProps} } }, "R4"],
    ]);
    my $blobId3 = $res->[0][1]{created}{c}{blobId};
    $self->assert_not_null($blobId3);

    xlog $self, "Different content must produce different blobId";
    $self->assert_str_not_equals($blobId1, $blobId3);
}
