<?php

namespace Tests\Unit\Models;

use App\Models\RemoteEmail;
use Carbon\Carbon;
use Tests\TestCase;

class RemoteEmailTest extends TestCase
{
    /** @test */
    public function it_can_create_a_remote_email_with_factory(): void
    {
        $remoteEmail = RemoteEmail::factory()->create();

        $this->assertInstanceOf(RemoteEmail::class, $remoteEmail);
        $this->assertIsArray($remoteEmail->to);
        $this->assertIsString($remoteEmail->from_email);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $timestamp = now();
        $remoteEmailData = [
            'message_id' => 'remote_123',
            'subject' => 'Remote Email Subject',
            'from_name' => 'Remote Sender',
            'from_email' => 'remote@example.com',
            'to' => ['recipient@example.com'],
            'body_html' => '<p>HTML content</p>',
            'body_text' => 'Text content',
            'is_seen' => false,
            'timestamp' => $timestamp,
        ];

        $remoteEmail = RemoteEmail::query()->create($remoteEmailData);

        foreach ($remoteEmailData as $key => $value) {
            if ($key === 'timestamp') {
                $this->assertInstanceOf(Carbon::class, $remoteEmail->$key);
                $this->assertEquals($timestamp->format('Y-m-d H:i:s'), $remoteEmail->$key->format('Y-m-d H:i:s'));
            } else {
                $this->assertEquals($value, $remoteEmail->$key);
            }
        }
    }

    /** @test */
    public function it_casts_to_field_to_array(): void
    {
        $to = ['test1@example.com', 'test2@example.com'];
        $remoteEmail = RemoteEmail::factory()->create(['to' => $to]);

        $this->assertIsArray($remoteEmail->to);
        $this->assertEquals($to, $remoteEmail->to);
    }

    /** @test */
    public function it_casts_timestamp_to_datetime(): void
    {
        $timestamp = now();
        $remoteEmail = RemoteEmail::factory()->create(['timestamp' => $timestamp]);

        $this->assertInstanceOf(Carbon::class, $remoteEmail->timestamp);
        $this->assertEquals($timestamp->format('Y-m-d H:i:s'), $remoteEmail->timestamp->format('Y-m-d H:i:s'));
    }
}
