<?php

namespace Tests\Unit;

use App\Models\ContentBlock;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ContentBlockTest extends TestCase
{
    use RefreshDatabase;

    public function test_for_page_uses_defaults_when_table_is_empty(): void
    {
        $blocks = ContentBlock::forPage('contact');

        $this->assertTrue($blocks->has('hero'));
        $this->assertSame("Step into\nthe gym.", $blocks['hero']->title);
        $this->assertSame('Questions about classes, memberships or corporate sessions? Drop us a line, or just turn up — the kettle\'s on and the bags are ready.', $blocks['hero']->body);
    }

    public function test_for_page_prefers_database_over_defaults(): void
    {
        ContentBlock::query()->create([
            'page' => 'contact',
            'section_key' => 'hero',
            'title' => 'Custom title',
            'body' => 'Custom body',
        ]);

        $blocks = ContentBlock::forPage('contact');

        $this->assertSame('Custom title', $blocks['hero']->title);
        $this->assertSame('Custom body', $blocks['hero']->body);
    }

    public function test_for_page_merges_missing_sections_from_defaults(): void
    {
        ContentBlock::query()->create([
            'page' => 'membership',
            'section_key' => 'hero',
            'title' => 'Only hero seeded',
            'body' => 'Hero body',
        ]);

        $blocks = ContentBlock::forPage('membership');

        $this->assertSame('Only hero seeded', $blocks['hero']->title);
        $this->assertTrue($blocks->has('intro_offer'));
        $this->assertTrue($blocks->has('cta'));
    }
}
