<?php

namespace Tests\Feature;

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

class TrainingFeedbackTest extends TestCase
{
    use RefreshDatabase;

    public function test_feedback_page_renders(): void
    {
        $this->get('/feedback')
            ->assertOk()
            ->assertSee('How did we do?')
            ->assertSee('Training Feedback')
            ->assertSee('Session Details');
    }

    public function test_feedback_page_accepts_share_link_context(): void
    {
        $this->get('/feedback?organization=Acme%20Ltd&event=Money%20Day&date=2026-07-03&facilitator=Rina&segment=Mid-level%20%2F%20supervisors')
            ->assertOk()
            ->assertSee('Acme Ltd')
            ->assertSee('Money Day')
            ->assertSee('2026-07-03')
            ->assertSee('Rina')
            ->assertSee('Mid-level / supervisors');
    }

    public function test_feedback_stores_data(): void
    {
        $response = $this->post('/feedback', [
            'name' => 'Jane Doe',
            'email' => 'jane@example.com',
            'organization' => 'Acme Ltd',
            'event_name' => 'Money Day',
            'training_date' => '2026-07-03 00:00:00',
            'facilitator' => 'Rina Hicks',
            'audience_segment' => 'Mid-level / supervisors',
            'content_satisfaction' => 5,
            'trainer_expertise' => 4,
            'scheduling_rating' => 3,
            'scheduling_comment' => 'Needs to be longer.',
            'trainer_personalization' => 5,
            'trainer_advice' => 'Yes, very clear.',
            'overall_experience' => 5,
            'financial_mindset_impact' => 'Helped me track my spending.',
            'recommend_training' => 'Highly recommend.',
            'greatest_impact_topics' => ['Managing Debt', 'Investing and Investment Options'],
            'more_time_topics' => ['Investing and Investment Options'],
            'recommended_improvements' => 'More practical examples.',
            'trainer_additional_feedback' => 'Great work.',
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('feedback_submitted');

        $this->assertDatabaseHas('training_feedbacks', [
            'name' => 'Jane Doe',
            'email' => 'jane@example.com',
            'organization' => 'Acme Ltd',
            'event_name' => 'Money Day',
            'training_date' => '2026-07-03 00:00:00',
            'facilitator' => 'Rina Hicks',
            'audience_segment' => 'Mid-level / supervisors',
            'content_satisfaction' => 5,
            'trainer_expertise' => 4,
            'scheduling_rating' => 3,
            'trainer_personalization' => 5,
            'overall_experience' => 5,
        ]);
    }

    public function test_feedback_validates_required_fields(): void
    {
        $response = $this->post('/feedback', [
            'name' => 'Jane Doe',
            'email' => 'jane@example.com',
        ]);

        $response->assertSessionHasErrors([
            'content_satisfaction',
            'trainer_expertise',
            'scheduling_rating',
            'trainer_personalization',
            'overall_experience',
        ]);
    }
}
