<?php

namespace Tests\Feature\ContentPages;

use App\Models\ContentPage;
use App\Services\Settings\SettingsService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ContentPageTest extends TestCase
{
    use RefreshDatabase;

    public function test_published_content_page_is_publicly_viewable(): void
    {
        $page = ContentPage::query()->create([
            'title' => 'Privacy Policy',
            'slug' => 'privacy-policy',
            'excerpt' => 'How RemoteMatch handles user data.',
            'content' => "# Privacy Policy\n\nYour resume data is processed securely.",
            'status' => ContentPage::STATUS_PUBLISHED,
            'show_in_footer' => true,
            'published_at' => now(),
        ]);

        $this
            ->get(route('content-pages.show', $page->slug))
            ->assertOk()
            ->assertSeeText('Privacy Policy')
            ->assertSeeText('How RemoteMatch handles user data.')
            ->assertSeeText('Your resume data is processed securely.')
            ->assertDontSee('<script>', false);
    }

    public function test_draft_content_page_is_not_publicly_viewable(): void
    {
        $page = ContentPage::query()->create([
            'title' => 'Draft Disclaimer',
            'slug' => 'draft-disclaimer',
            'content' => 'Draft content.',
            'status' => ContentPage::STATUS_DRAFT,
        ]);

        $this
            ->get(route('content-pages.show', $page->slug))
            ->assertNotFound();
    }

    public function test_landing_footer_lists_published_footer_pages(): void
    {
        ContentPage::query()->create([
            'title' => 'Terms and Conditions',
            'slug' => 'terms-and-conditions',
            'content' => 'Terms content.',
            'status' => ContentPage::STATUS_PUBLISHED,
            'show_in_footer' => true,
            'footer_label' => 'Terms',
            'footer_sort' => 1,
            'published_at' => now(),
        ]);

        $this
            ->get(route('landing'))
            ->assertOk()
            ->assertSeeText('Content pages')
            ->assertSeeText('Terms')
            ->assertSee('/pages/terms-and-conditions', false);
    }

    public function test_content_pages_use_shared_admin_branding(): void
    {
        app(SettingsService::class)->set('general', 'application_name', 'TalentPilot', 'string', false, true);
        app(SettingsService::class)->set('branding', 'logo', 'settings/talentpilot-logo.png', 'file', false, true);
        app(SettingsService::class)->set('branding', 'favicon', 'settings/talentpilot-favicon.png', 'file', false, true);

        $page = ContentPage::query()->create([
            'title' => 'Disclaimer',
            'slug' => 'disclaimer',
            'content' => 'No job placement guarantee.',
            'status' => ContentPage::STATUS_PUBLISHED,
            'published_at' => now(),
        ]);

        $this
            ->get(route('content-pages.show', $page->slug))
            ->assertOk()
            ->assertSee('settings/talentpilot-logo.png', false)
            ->assertSee('settings/talentpilot-favicon.png', false)
            ->assertSeeText('TalentPilot');
    }
}
