<?php

namespace Tests\Feature\Api\V1\Frontend;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class PageControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_privacy_returns_content(): void
    {
        $dir = public_path('html');
        File::ensureDirectoryExists($dir);
        File::put($dir . '/privacy_policy.html', '<h1>Privacy</h1>');

        $response = $this->getJson('/api/v1/pages/privacy');

        $response->assertOk();
        $response->assertJsonPath('data.page.type', 'privacy_policy');
        $this->assertStringContainsString('Privacy', $response->json('data.page.content'));
    }

    public function test_help_missing_returns_error(): void
    {
        $dir = public_path('html');
        File::ensureDirectoryExists($dir);
        File::delete($dir . '/help_center.html');

        $response = $this->getJson('/api/v1/pages/help');

        $response->assertStatus(404);
    }

    public function test_submit_contact_persists(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        $payload = [
            'email' => 'contact@example.com',
            'message' => 'Hello there, this is a test message.',
        ];

        $response = $this->postJson('/api/v1/pages/contact', $payload);

        $response->assertStatus(201);
        $this->assertDatabaseHas('contactus', [
            'subject' => 'Contact Us Form Submission',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
