<?php

namespace Tests\Feature\Api;

use App\Models\User;

class AuthenticationTest extends ApiTestCase
{
    public function test_user_can_register(): void
    {
        $branch = $this->createBranch();

        $response = $this->postJson('/api/v1/auth/register', [
            'name' => 'New User',
            'email' => 'newuser@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'branch_id' => $branch->id,
        ], $this->apiHeaders());

        $response->assertStatus(201);
        $response->assertJsonStructure([
            'success',
            'message',
            'data' => [
                'user' => ['id', 'name', 'email'],
                'token',
            ],
        ]);

        $this->assertDatabaseHas('users', [
            'email' => 'newuser@example.com',
            'name' => 'New User',
        ]);
    }

    public function test_registration_requires_valid_email(): void
    {
        $branch = $this->createBranch();

        $response = $this->postJson('/api/v1/auth/register', [
            'name' => 'New User',
            'email' => 'invalid-email',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'branch_id' => $branch->id,
        ], $this->apiHeaders());

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

    public function test_registration_requires_password_confirmation(): void
    {
        $branch = $this->createBranch();

        $response = $this->postJson('/api/v1/auth/register', [
            'name' => 'New User',
            'email' => 'test@example.com',
            'password' => 'password123',
            'password_confirmation' => 'different_password',
            'branch_id' => $branch->id,
        ], $this->apiHeaders());

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

    public function test_user_can_login_with_valid_credentials(): void
    {
        $branch = $this->createBranch();
        $user = User::create([
            'name' => 'Test User',
            'email' => 'login@example.com',
            'password' => bcrypt('password123'),
            'branch_id' => $branch->id,
        ]);

        $response = $this->postJson('/api/v1/auth/login', [
            'email' => 'login@example.com',
            'password' => 'password123',
        ], $this->apiHeaders());

        $response->assertStatus(200);
        $response->assertJsonStructure([
            'success',
            'message',
            'data' => [
                'user' => ['id', 'name', 'email'],
                'token',
            ],
        ]);
    }

    public function test_login_fails_with_invalid_credentials(): void
    {
        $branch = $this->createBranch();
        User::create([
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => bcrypt('password123'),
            'branch_id' => $branch->id,
        ]);

        $response = $this->postJson('/api/v1/auth/login', [
            'email' => 'test@example.com',
            'password' => 'wrong_password',
        ], $this->apiHeaders());

        $response->assertStatus(422);
        $response->assertJson(['success' => false]);
    }

    public function test_authenticated_user_can_get_profile(): void
    {
        $this->createAuthenticatedUser(['email' => 'profile@example.com']);

        $response = $this->getJson('/api/v1/auth/user');

        $response->assertStatus(200);
        $response->assertJson([
            'success' => true,
            'data' => [
                'email' => 'profile@example.com',
            ],
        ]);
    }

    public function test_unauthenticated_user_cannot_get_profile(): void
    {
        $response = $this->getJson('/api/v1/auth/user', $this->apiHeaders());

        $response->assertStatus(401);
    }

    public function test_user_can_logout(): void
    {
        $branch = $this->createBranch();
        $user = User::create([
            'name' => 'Test User',
            'email' => 'logout@example.com',
            'password' => bcrypt('password123'),
            'branch_id' => $branch->id,
        ]);

        $token = $user->createToken('test-token')->plainTextToken;

        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Authorization' => 'Bearer ' . $token,
        ])->postJson('/api/v1/auth/logout');

        $response->assertStatus(200);
        $response->assertJson(['success' => true]);
    }

    public function test_user_can_refresh_token(): void
    {
        $branch = $this->createBranch();
        $user = User::create([
            'name' => 'Test User',
            'email' => 'refresh@example.com',
            'password' => bcrypt('password123'),
            'branch_id' => $branch->id,
        ]);

        $token = $user->createToken('test-token')->plainTextToken;

        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Authorization' => 'Bearer ' . $token,
        ])->postJson('/api/v1/auth/refresh-token');

        $response->assertStatus(200);
        $response->assertJsonStructure([
            'success',
            'message',
            'data' => ['token'],
        ]);
    }
}
