<?php

namespace Tests\Feature;

use App\Models\Sponsor;
use App\Models\User;
use App\Models\EmployerProfile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class SponsorAuthWebTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test successful multi-step web employer registration with Emirates ID upload.
     */
    public function test_employer_web_registration_with_emirates_id()
    {
        Storage::fake('public');

        $emiratesIdFile = UploadedFile::fake()->create('emirates_id.pdf', 500, 'application/pdf');

        // Step 1: Registration Form Submission
        $responseStep1 = $this->post('/employer/register', [
            'name' => 'Abdullah Ahmed',
            'email' => 'abdullah@example.com',
            'phone' => '501234567',
            'emirates_id_file' => $emiratesIdFile,
        ]);

        $responseStep1->assertRedirect(route('employer.verify-email'));

        // Verify session data and file presence
        $pendingReg = session('pending_employer_registration');
        $this->assertNotNull($pendingReg);
        $this->assertEquals('Abdullah Ahmed', $pendingReg['name']);
        $this->assertNotNull($pendingReg['emirates_id_file']);
        $this->assertStringContainsString('uploads/licenses/', $pendingReg['emirates_id_file']);

        // Step 2: OTP Verification (using local environment dummy code)
        $responseStep2 = $this->post('/employer/verify-email', [
            'otp' => '000000',
        ]);

        $responseStep2->assertRedirect(route('employer.register-payment'));
        $this->assertTrue(session('employer_email_verified'));

        // Step 3: Payment Choice
        $responseStep3 = $this->post('/employer/register-payment', [
            'plan_id' => 'premium',
            'amount_aed' => 199.00,
            'paytabs_transaction_id' => 'TRAN_ABC_123',
        ]);

        $responseStep3->assertJson(['message' => 'Payment processed successfully.']);

        // Step 4: Create Password & Create Records
        $responseStep4 = $this->post('/employer/create-password', [
            'password' => 'SecurePass123!',
            'password_confirmation' => 'SecurePass123!',
        ]);

        $responseStep4->assertRedirect(route('employer.dashboard'));

        // Verify Database Records
        $this->assertDatabaseHas('users', [
            'email' => 'abdullah@example.com',
            'role' => 'employer',
        ]);

        $this->assertDatabaseHas('employer_profiles', [
            'phone' => '501234567',
            'company_name' => 'Abdullah Ahmed Household',
        ]);

        $this->assertDatabaseHas('sponsors', [
            'email' => 'abdullah@example.com',
            'mobile' => '501234567',
        ]);

        // Check Sponsor has the Emirates ID path matching the session stored file
        $sponsor = Sponsor::where('email', 'abdullah@example.com')->first();
        $this->assertNotNull($sponsor->emirates_id_file);
        $this->assertEquals($pendingReg['emirates_id_file'], $sponsor->emirates_id_file);
    }
}
