<?php

namespace Tests\Feature\Landing;

use App\Models\ImportedJob;
use App\Models\JobSource;
use App\Services\Settings\SettingsService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class LandingPageComplianceTest extends TestCase
{
    use RefreshDatabase;

    public function test_landing_page_does_not_show_price_or_currency(): void
    {
        $response = $this->get(route('landing'));

        $response
            ->assertOk()
            ->assertDontSeeText('₹')
            ->assertDontSeeText('$')
            ->assertDontSeeText('INR')
            ->assertDontSeeText('USD')
            ->assertDontSeeText('pricing')
            ->assertDontSeeText('price');
    }

    public function test_landing_page_mentions_broad_remote_job_categories(): void
    {
        $response = $this->get(route('landing'));

        foreach ([
            'Tech',
            'Design',
            'Marketing',
            'Sales',
            'Writing',
            'Support',
            'Product',
            'Operations',
            'Virtual assistant',
            'Finance',
            'HR',
            'Data',
            'AI',
            'General remote jobs',
        ] as $category) {
            $response->assertSeeText($category);
        }
    }

    public function test_landing_page_shows_real_active_job_previews_without_sensitive_urls(): void
    {
        $source = JobSource::query()->create([
            'name' => 'Remote Feed',
            'slug' => 'remote-feed',
            'type' => 'api',
            'base_url' => 'https://remote-feed.example',
            'feed_url' => 'https://remote-feed.example/feed',
            'attribution_text' => 'Remote Feed',
            'is_active' => true,
        ]);

        $job = ImportedJob::query()->create([
            'job_source_id' => $source->id,
            'external_id' => 'public-preview-001',
            'title' => 'Remote Lifecycle Marketing Manager',
            'company' => 'SignalWorks',
            'category' => 'marketing',
            'employment_type' => 'full_time',
            'experience_level' => 'mid',
            'location_type' => 'remote',
            'summary' => 'Own lifecycle campaigns for a distributed SaaS team.',
            'description' => 'Full description should not be exposed on the homepage.',
            'skills' => ['lifecycle', 'email marketing', 'analytics', 'crm'],
            'tags' => ['remote-first', 'growth', 'saas'],
            'salary_currency' => 'USD',
            'salary_min' => 70000,
            'salary_max' => 95000,
            'source_url' => 'https://remote-feed.example/jobs/public-preview-001',
            'apply_url' => 'https://remote-feed.example/jobs/public-preview-001/apply?token=secret',
            'source_attribution' => 'Remote Feed',
            'status' => ImportedJob::STATUS_ACTIVE,
            'posted_at' => now(),
            'expires_at' => now()->addMonth(),
            'last_seen_at' => now(),
        ]);

        $response = $this->get(route('landing'));

        $response
            ->assertOk()
            ->assertSeeText('Remote Lifecycle Marketing Manager')
            ->assertSeeText('SignalWorks')
            ->assertSeeText('Remote Feed')
            ->assertSeeText('$70,000 - $95,000')
            ->assertSeeText('remote-first')
            ->assertSeeText('growth')
            ->assertSeeText('Preview only')
            ->assertSeeText('Apply link locked')
            ->assertDontSee($job->source_url, false)
            ->assertDontSee($job->apply_url, false)
            ->assertDontSeeText('Full description should not be exposed on the homepage.')
            ->assertDontSeeText('Apply Now')
            ->assertDontSeeText('View Details');
    }

    public function test_landing_page_shows_empty_state_when_no_active_jobs_exist(): void
    {
        $response = $this->get(route('landing'));

        $response
            ->assertOk()
            ->assertSeeText('Fresh remote roles are being updated.')
            ->assertSeeText('Complete your profile to get personalized matches.');
    }

    public function test_landing_page_uses_admin_branding_settings(): void
    {
        Storage::fake('public');
        Storage::disk('public')->put('settings/talentpilot-logo.png', $this->pngContent(612, 408));

        $settings = app(SettingsService::class);

        $settings->set('general', 'application_name', 'TalentPilot', 'string', false, true);
        $settings->set('general', 'application_tagline', 'Smarter remote career matching', 'string', false, true);
        $settings->set('branding', 'logo', 'settings/talentpilot-logo.png', 'file', false, true);
        $settings->set('branding', 'logo_width_px', 156, 'integer', false, true);
        $settings->set('branding', 'logo_height_px', 150, 'integer', false, true);
        $settings->set('branding', 'favicon', 'settings/talentpilot-favicon.png', 'file', false, true);
        $settings->set('branding', 'landing_page_hero_headline', 'Remote roles ranked for your profile', 'string', false, true);
        $settings->set('branding', 'landing_page_subheadline', 'Upload your resume and unlock profile-aware remote job discovery.', 'textarea', false, true);
        $settings->set('branding', 'landing_page_cta_text', 'Get Matched', 'string', false, true);
        $settings->set('branding', 'footer_description', 'Custom footer description from admin settings.', 'textarea', false, true);
        $settings->set('general', 'footer_copyright_text', 'Copyright TalentPilot', 'string', false, true);

        $this
            ->get(route('landing'))
            ->assertOk()
            ->assertSee('settings/talentpilot-logo.png', false)
            ->assertSee('width: 225px; height: 150px;', false)
            ->assertSee('settings/talentpilot-favicon.png', false)
            ->assertSeeText('TalentPilot')
            ->assertSeeText('Smarter remote career matching')
            ->assertSeeText('Remote roles ranked for your profile')
            ->assertSeeText('Upload your resume and unlock profile-aware remote job discovery.')
            ->assertSeeText('Get Matched')
            ->assertSeeText('Custom footer description from admin settings.')
            ->assertSeeText('Copyright TalentPilot');
    }

    private function pngContent(int $width, int $height): string
    {
        $image = imagecreatetruecolor($width, $height);

        ob_start();
        imagepng($image);
        $content = (string) ob_get_clean();

        imagedestroy($image);

        return $content;
    }
}
