<?php

namespace Tests\Feature;

use App\Models\User;
use App\Services\ContactSettingService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ContactSettingsTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_update_contact_settings(): void
    {
        $admin = User::factory()->create();

        $this->withoutMiddleware()
            ->actingAs($admin)
            ->put(route('admin.settings.contact.update'), [
                'support_phone' => '07065620433',
                'support_email' => 'support@example.com',
                'whatsapp_number' => '07065620433',
                'whatsapp_group_url' => 'https://chat.whatsapp.com/test-group',
                'telegram_url' => 'https://t.me/vtu_support',
                'website_url' => 'https://example.com',
                'office_address' => 'Kano, Nigeria',
                'business_hours' => 'Mon-Sat 8AM-6PM',
                'support_note' => 'Contact support only from registered phone number.',
            ])
            ->assertSessionHasNoErrors()
            ->assertSessionHas('success');

        $contact = app(ContactSettingService::class)->get();

        $this->assertSame('07065620433', $contact['support_phone']);
        $this->assertSame('support@example.com', $contact['support_email']);
        $this->assertSame('https://wa.me/2347065620433', $contact['whatsapp_chat_url']);
        $this->assertSame('https://chat.whatsapp.com/test-group', $contact['whatsapp_group_url']);
        $this->assertSame('https://example.com', $contact['website_url']);
    }

    public function test_user_can_open_contact_page(): void
    {
        $user = User::factory()->create();

        app(ContactSettingService::class)->update([
            'support_phone' => '07065620433',
            'support_email' => 'support@example.com',
            'whatsapp_number' => '07065620433',
            'whatsapp_group_url' => 'https://chat.whatsapp.com/test-group',
            'support_note' => 'We reply quickly on WhatsApp.',
        ]);

        $this->withoutMiddleware()
            ->actingAs($user)
            ->get(route('app.contact.index'))
            ->assertOk()
            ->assertSee('App\\/Contact\\/Index', false)
            ->assertSee('07065620433')
            ->assertSee('support@example.com');
    }
}
