<?php

namespace Tests\Feature\Questionnaire;

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

class QuestionnaireValidationTest extends TestCase
{
    use RefreshDatabase;

    public function test_valid_questionnaire_submission_is_stored_and_redirects_to_checkout(): void
    {
        $user = User::factory()->create(['questionnaire_completed_at' => null]);

        $response = $this
            ->actingAs($user)
            ->post(route('questionnaire.store'), $this->validPayload([
                'country_code' => 'in',
                'salary_currency' => 'usd',
            ]));

        $response->assertRedirect(route('payment.checkout'));

        $this->assertNotNull($user->fresh()->questionnaire_completed_at);
        $this->assertDatabaseHas('questionnaire_responses', [
            'user_id' => $user->id,
            'experience_level' => 'mid',
            'availability' => 'immediate',
            'country_code' => 'IN',
            'salary_currency' => 'USD',
            'salary_min' => 2500,
            'open_to_contract' => true,
            'open_to_full_time' => true,
        ]);
    }

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

        $response = $this
            ->actingAs($user)
            ->from(route('questionnaire.show'))
            ->post(route('questionnaire.store'), $this->validPayload([
                'preferred_categories' => ['tech', 'onsite-only'],
            ]));

        $response
            ->assertRedirect(route('questionnaire.show'))
            ->assertSessionHasErrors('preferred_categories.1');

        $this->assertNull($user->fresh()->questionnaire_completed_at);
        $this->assertDatabaseCount('questionnaire_responses', 0);
    }

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

        $response = $this
            ->actingAs($user)
            ->from(route('questionnaire.show'))
            ->post(route('questionnaire.store'), $this->validPayload([
                'preferred_categories' => [
                    'tech',
                    'design',
                    'marketing',
                    'sales',
                    'writing',
                    'support',
                    'product',
                    'operations',
                    'finance',
                ],
            ]));

        $response
            ->assertRedirect(route('questionnaire.show'))
            ->assertSessionHasErrors('preferred_categories');

        $this->assertNull($user->fresh()->questionnaire_completed_at);
        $this->assertDatabaseCount('questionnaire_responses', 0);
    }

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

        $response = $this
            ->actingAs($user)
            ->from(route('questionnaire.show'))
            ->post(route('questionnaire.store'), $this->validPayload([
                'preferred_roles' => ['Made Up Role'],
                'skills' => ['Made Up Skill'],
            ]));

        $response
            ->assertRedirect(route('questionnaire.show'))
            ->assertSessionHasErrors(['preferred_roles.0', 'skills.0']);

        $this->assertNull($user->fresh()->questionnaire_completed_at);
        $this->assertDatabaseCount('questionnaire_responses', 0);
    }

    /**
     * @param  array<string, mixed>  $overrides
     * @return array<string, mixed>
     */
    private function validPayload(array $overrides = []): array
    {
        return array_replace_recursive([
            'preferred_categories' => ['tech', 'marketing', 'operations'],
            'preferred_roles' => ['Product Manager', 'Digital Marketer'],
            'skills' => ['Remote communication', 'SEO', 'Data analysis'],
            'experience_level' => 'mid',
            'availability' => 'immediate',
            'preferred_timezone' => 'Asia/Kolkata',
            'country_code' => 'IN',
            'salary_currency' => 'USD',
            'salary_min' => 2500,
            'open_to_contract' => '1',
            'open_to_full_time' => '1',
            'answers' => [
                'source' => 'questionnaire_v2_multiple_choice',
                'mode' => 'multiple_choice',
            ],
        ], $overrides);
    }
}
