<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ConsultationTest extends TestCase
{
    use RefreshDatabase;

    public function test_consultation_page_renders(): void
    {
        $this->get('/consultation')
            ->assertOk()
            ->assertSee('Book a consultation')
            ->assertSee('Who are we talking to?');
    }

    public function test_booking_stores_consultation_and_lead(): void
    {
        $this->postJson('/consultation', [
            'name' => 'Amina Otieno',
            'email' => 'amina@example.com',
            'phone' => '+254700000000',
            'country' => 'Kenya',
            'segment' => 'individual',
            'topic' => 'debt',
            'contact_method' => 'whatsapp',
            'preferred_time' => 'morning',
            'message' => 'I want a payoff plan.',
        ])->assertOk()->assertJson(['booked' => true]);

        $this->assertDatabaseHas('consultations', [
            'email' => 'amina@example.com',
            'segment' => 'individual',
            'topic' => 'debt',
            'status' => 'new',
        ]);

        $this->assertDatabaseHas('leads', ['email' => 'amina@example.com', 'source' => 'consultation']);
    }

    public function test_booking_validates_required_fields(): void
    {
        $this->postJson('/consultation', [
            'name' => 'X',
            'email' => 'not-an-email',
            'segment' => 'alien',
            'topic' => 'debt',
            'contact_method' => 'pigeon',
            'preferred_time' => 'midnight',
        ])->assertStatus(422)
            ->assertJsonValidationErrors(['email', 'segment', 'contact_method', 'preferred_time']);
    }

    public function test_course_catalogue_shows_published_courses_only(): void
    {
        $this->seed(\Database\Seeders\CourseSeeder::class);
        $this->seed(\Database\Seeders\MiniCoursesSeeder::class);

        $this->get('/learn')
            ->assertOk()
            ->assertSee('Investable')                       // flagship, published
            ->assertSee('Crypto Basics')                    // academy mini-course, published
            ->assertSee('Retirement Benefits Options')      // academy mini-course, published
            ->assertDontSee('The Debt-Free Blueprint')      // placeholder, now a draft
            ->assertDontSee('Budgeting Mastery')            // placeholder, now a draft
            ->assertDontSee('The Diaspora Investor');       // placeholder, now a draft
    }
}
