<?php

namespace Tests\Unit\Models;

use App\Models\Meta;
use Tests\TestCase;

class MetaTest extends TestCase
{
    /** @test */
    public function it_can_create_a_meta_with_factory(): void
    {
        $meta = Meta::factory()->create();

        $this->assertInstanceOf(Meta::class, $meta);
        $this->assertIsString($meta->key);
        $this->assertIsString($meta->value);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $metaData = [
            'key' => 'total_emails_created',
            'value' => '1500',
        ];

        $meta = Meta::query()->create($metaData);

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

    /** @test */
    public function it_stores_key_value_pairs_correctly(): void
    {
        $meta = Meta::factory()->create([
            'key' => 'app_version',
            'value' => '1.2.3',
        ]);

        $this->assertEquals('app_version', $meta->key);
        $this->assertEquals('1.2.3', $meta->value);
    }

    /** @test */
    public function it_can_retrieve_value_by_key(): void
    {
        Meta::factory()->create(['key' => 'site_name', 'value' => 'ZEmailnator']);
        Meta::factory()->create(['key' => 'max_emails', 'value' => '100']);

        $siteName = Meta::query()->where('key', 'site_name')->first();
        $maxEmails = Meta::query()->where('key', 'max_emails')->first();

        $this->assertEquals('ZEmailnator', $siteName->value);
        $this->assertEquals('100', $maxEmails->value);
    }
}
