<?php

namespace Tests\Unit\Models;

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

class MessageTest extends TestCase
{
    /** @test */
    public function it_can_create_a_message_with_factory(): void
    {
        $message = Message::factory()->create();

        $this->assertInstanceOf(Message::class, $message);
        $this->assertIsString($message->subject);
        $this->assertIsString($message->from);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $messageData = [
            'subject' => 'Test Message',
            'from' => 'Test Sender <sender@example.com>',
            'to' => 'recipient@example.com',
            'body' => 'Test body content',
            'attachments' => null,
            'is_seen' => false,
        ];

        $message = Message::query()->create($messageData);

        foreach ($messageData as $key => $value) {
            $this->assertEquals($value, $message->$key);
        }
    }

    /** @test */
    public function it_stores_to_field_as_string(): void
    {
        $to = 'test1@example.com';
        $message = Message::factory()->create(['to' => $to]);

        $this->assertIsString($message->to);
        $this->assertEquals($to, $message->to);
    }

    /** @test */
    public function it_uses_created_at_as_timestamp(): void
    {
        $message = Message::factory()->create();

        $this->assertInstanceOf(Carbon::class, $message->created_at);
        $this->assertNotNull($message->created_at);
    }

    /** @test */
    public function it_can_query_unseen_messages(): void
    {
        Message::factory()->create(['is_seen' => false]);
        Message::factory()->create(['is_seen' => true]);

        $unseenMessages = Message::query()->where('is_seen', false)->get();
        $seenMessages = Message::query()->where('is_seen', true)->get();

        $this->assertCount(1, $unseenMessages);
        $this->assertCount(1, $seenMessages);
    }
}
