<?php

use App\Models\Auth\Verification;
use App\Models\User;

test('guest token can be generated', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/guest-token');

    $response->assertStatus(201)
        ->assertJsonStructure([
            'data',
            'message',
        ]);

    $this->assertNotNull($response->json('data'));
    // Check if a guest user was created
    $this->assertDatabaseHas('users', [
        'first_name' => 'Guest',
        'last_name' => 'User',
    ]);
});

test('user can register', function () {
    $payload = [
        'phone_number' => 61929248,
        'name' => 'Test User',
        'address' => 'Test Address',
    ];

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/register', $payload);

    $response->assertStatus(201);

    $this->assertDatabaseHas('users', [
        'phone_number' => 61929248,
        'first_name' => 'Test',
        'last_name' => 'User',
    ]);

    $this->assertDatabaseHas('verifications', [
        'username' => 61929248,
    ]);
});

test('register validation fails with invalid data', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/register', [
            'phone_number' => 'invalid',
        ]);

    $response->assertStatus(422)
        ->assertJsonValidationErrors(['phone_number', 'name', 'address']);
});

test('register fails if phone already exists', function () {
    User::factory()->create([
        'phone_number' => 61929248,
        'password' => 'password',
    ]);

    $payload = [
        'phone_number' => 61929248,
        'name' => 'Test User',
        'address' => 'Test Address',
    ];

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/register', $payload);

    $response->assertStatus(422)
        ->assertJsonValidationErrors(['phone_number']);
});

test('user can login', function () {
    $user = User::factory()->create([
        'phone_number' => 61929248,
        'password' => 'password',
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/login', [
            'phone_number' => 61929248,
        ]);

    $response->assertStatus(201);

    $this->assertDatabaseHas('verifications', [
        'username' => 61929248,
    ]);
});

test('login fails if user does not exist', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/login', [
            'phone_number' => 61929248,
        ]);

    $response->assertStatus(422)
        ->assertJsonValidationErrors(['phone_number']);
});

test('user can verify code and get token', function () {
    $phone = 61929248;
    $user = User::factory()->create([
        'phone_number' => $phone,
        'password' => 'password',
    ]);

    // Create verification code
    Verification::create([
        'username' => $phone,
        'code' => 12345,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/verify', [
            'phone_number' => $phone,
            'code' => 12345,
        ]);

    $response->assertStatus(201)
        ->assertJsonStructure(['data']);

    $this->assertNotNull($response->json('data'));
});

test('verify fails with incorrect code', function () {
    $phone = 61929248;
    User::factory()->create([
        'phone_number' => $phone,
        'password' => 'password',
    ]);

    Verification::create([
        'username' => $phone,
        'code' => 12345,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/verify', [
            'phone_number' => $phone,
            'code' => 54321,
        ]);

    $response->assertStatus(422)
        ->assertJsonValidationErrors(['code']);
});

test('authenticated user can delete account', function () {
    $user = User::factory()->create([
        'password' => 'password',
    ]);

    $response = $this->actingAs($user, 'sanctum')
        ->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/delete-user');

    $response->assertStatus(200); // or 204 depending on implementation

    $this->assertDatabaseMissing('users', [
        'id' => $user->id,
    ]);
});

test('unauthenticated user cannot delete account', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->postJson('/api/v1/auth/delete-user');

    $response->assertStatus(401);
});
