<?php

namespace Tests\Feature;

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

class CvMakerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test registration creates user and sends OTP simulation.
     */
    public function test_user_can_register_and_receives_whatsapp_otp()
    {
        $response = $this->post('/register', [
            'name' => 'Budi Santoso',
            'email' => 'budi@example.com',
            'whatsapp' => '081234567890',
            'password' => 'password123',
            'password_confirmation' => 'password123',
        ]);

        $response->assertStatus(200);
        $response->assertJsonStructure(['success', 'message', 'redirect']);

        // Assert user exists in database with unverified WhatsApp
        $this->assertDatabaseHas('users', [
            'email' => 'budi@example.com',
            'whatsapp' => '6281234567890',
            'whatsapp_verified_at' => null,
        ]);

        $user = User::where('email', 'budi@example.com')->first();
        $this->assertNotNull($user->otp_code);
    }

    /**
     * Test user can verify their WhatsApp using correct OTP.
     */
    public function test_user_can_verify_whatsapp_otp()
    {
        $user = User::create([
            'name' => 'Budi Santoso',
            'email' => 'budi@example.com',
            'whatsapp' => '6281234567890',
            'password' => bcrypt('password123'),
            'otp_code' => '123456',
            'otp_expires_at' => now()->addMinutes(5),
        ]);

        $this->actingAs($user);

        $response = $this->post('/verify', [
            'otp' => '123456',
        ]);

        $response->assertStatus(200);
        $response->assertJson([
            'success' => true,
            'message' => 'WhatsApp berhasil diverifikasi!',
        ]);

        $this->assertNotNull($user->fresh()->whatsapp_verified_at);
    }

    /**
     * Test CV creation, updates, and deletion.
     */
    public function test_verified_user_can_create_and_manage_cv()
    {
        $user = User::create([
            'name' => 'Budi Santoso',
            'email' => 'budi@example.com',
            'whatsapp' => '6281234567890',
            'password' => bcrypt('password123'),
            'whatsapp_verified_at' => now(),
        ]);

        $this->actingAs($user);

        // 1. Create CV
        $cvData = [
            'title' => 'CV Web Developer',
            'content' => [
                'contact' => [
                    'name' => 'Budi Santoso',
                    'email' => 'budi@example.com',
                    'phone' => '6281234567890',
                    'location' => 'Jakarta',
                    'summary' => 'Saya developer berbakat.',
                ],
                'experience' => [
                    [
                        'company' => 'Tech Corp',
                        'role' => 'Web Developer',
                        'start' => '2022',
                        'end' => '2024',
                        'description' => 'Membangun API Laravel.',
                    ]
                ],
                'skills' => ['PHP', 'Laravel', 'HTML'],
            ]
        ];

        $response = $this->post('/cv', $cvData);
        $response->assertStatus(200);
        $response->assertJson(['success' => true]);

        $this->assertDatabaseHas('cvs', [
            'user_id' => $user->id,
            'title' => 'CV Web Developer',
        ]);

        $cv = Cv::first();

        // 2. Update CV
        $updatedData = $cvData;
        $updatedData['title'] = 'CV Laravel Developer';
        
        $response = $this->put("/cv/{$cv->id}", $updatedData);
        $response->assertStatus(200);
        $this->assertEquals('CV Laravel Developer', $cv->fresh()->title);

        // 3. Delete CV
        $response = $this->delete("/cv/{$cv->id}");
        $response->assertStatus(200);
        $this->assertDatabaseMissing('cvs', ['id' => $cv->id]);
    }
}
