<?php

namespace Tests\Unit\Models;

use App\Models\UsageLog;
use App\Models\User;
use Tests\TestCase;

class UsageLogTest extends TestCase
{
    public $user;
    protected function setUp(): void
    {
        parent::setUp();

        $this->user = User::factory()->create();
    }

    /** @test */
    public function it_can_create_a_usage_log_with_factory(): void
    {
        $usageLog = UsageLog::factory()->create();

        $this->assertInstanceOf(UsageLog::class, $usageLog);
        $this->assertIsInt($usageLog->emails_created_count);
        $this->assertIsInt($usageLog->emails_received_count);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $usageLogData = [
            'user_id' => $this->user->id,
            'ip_address' => '192.168.1.1',
            'emails_created_count' => 5,
            'emails_received_count' => 10,
            'emails_created_history' => json_encode(['2023-01-01 12:00:00' => 3]),
            'emails_received_history' => json_encode(['2023-01-01 12:30:00' => 7]),
        ];

        $usageLog = UsageLog::query()->create($usageLogData);

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

    /** @test */
    public function it_belongs_to_a_user(): void
    {
        $usageLog = UsageLog::factory()->create(['user_id' => $this->user->id]);

        $this->assertInstanceOf(User::class, $usageLog->user);
        $this->assertEquals($this->user->id, $usageLog->user->id);
    }

    /** @test */
    public function it_tracks_different_email_counts(): void
    {
        $usageLog = UsageLog::factory()->create([
            'emails_created_count' => 15,
            'emails_received_count' => 25,
        ]);

        $this->assertEquals(15, $usageLog->emails_created_count);
        $this->assertEquals(25, $usageLog->emails_received_count);
    }
}
