<?php

namespace Tests\Feature\Profile;

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

class ProfileCompletionTest extends TestCase
{
    use RefreshDatabase;

    public function test_users_without_resume_analysis_are_redirected_before_profile(): void
    {
        $user = User::factory()->create([
            'questionnaire_completed_at' => now(),
            'access_unlocked_at' => now(),
            'resume_analyzed_at' => null,
        ]);

        $response = $this->actingAs($user)->get(route('profile.edit'));

        $response->assertRedirect(route('resume.show'));
    }

    public function test_valid_profile_submission_completes_profile_and_redirects_to_applicant_dashboard(): void
    {
        $user = $this->profileReadyUser();

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

        $response->assertRedirect(route('applicant.dashboard'));

        $this->assertNotNull($user->fresh()->profile_completed_at);
        $this->assertDatabaseHas('profiles', [
            'user_id' => $user->id,
            'headline' => 'Remote growth marketer',
            'experience_level' => 'mid',
            'country_code' => 'IN',
            'salary_currency' => 'USD',
            'salary_min' => 2500,
            'open_to_contract' => true,
            'open_to_full_time' => true,
        ]);
    }

    public function test_invalid_profile_submission_is_rejected(): void
    {
        $user = $this->profileReadyUser();

        $response = $this
            ->actingAs($user)
            ->from(route('profile.edit'))
            ->post(route('profile.update'), $this->validPayload([
                'headline' => '',
                'links' => ['not-a-url'],
            ]));

        $response
            ->assertRedirect(route('profile.edit'))
            ->assertSessionHasErrors(['headline', 'links.0']);

        $this->assertNull($user->fresh()->profile_completed_at);
        $this->assertDatabaseCount('profiles', 0);
    }

    private function profileReadyUser(): User
    {
        return User::factory()->create([
            'questionnaire_completed_at' => now(),
            'access_unlocked_at' => now(),
            'resume_analyzed_at' => now(),
            'profile_completed_at' => null,
        ]);
    }

    /**
     * @param  array<string, mixed>  $overrides
     * @return array<string, mixed>
     */
    private function validPayload(array $overrides = []): array
    {
        return array_replace_recursive([
            'headline' => 'Remote growth marketer',
            'summary' => 'Growth marketer focused on remote SaaS teams.',
            'current_role' => 'Growth Marketer',
            'experience_level' => 'mid',
            'country_code' => 'IN',
            'city' => 'Kolkata',
            'timezone' => 'Asia/Kolkata',
            'skills' => ['SEO', 'Analytics', 'Lifecycle marketing'],
            'preferred_roles' => ['Growth Marketer', 'Marketing Manager'],
            'preferred_categories' => ['marketing', 'sales'],
            'links' => ['https://example.test/portfolio'],
            'availability' => 'immediate',
            'salary_currency' => 'USD',
            'salary_min' => 2500,
            'open_to_contract' => '1',
            'open_to_full_time' => '1',
        ], $overrides);
    }
}
