<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;

uses(RefreshDatabase::class);

it('returns nutrition goals with the authenticated user', function () {
    $user = User::factory()->create([
        'daily_calorie_goal' => 2400,
        'daily_protein_goal' => 140,
        'daily_carbs_goal' => 280,
        'daily_fats_goal' => 80,
    ]);

    Sanctum::actingAs($user);

    $this->getJson('/api/auth/me')
        ->assertOk()
        ->assertJsonPath('data.nutritionGoals.calories', 2400)
        ->assertJsonPath('data.nutritionGoals.proteins', 140)
        ->assertJsonPath('data.nutritionGoals.carbs', 280)
        ->assertJsonPath('data.nutritionGoals.fats', 80);
});

it('updates nutrition goals for the authenticated user', function () {
    $user = User::factory()->create();

    Sanctum::actingAs($user);

    $response = $this->patchJson('/api/auth/me', [
        'nutritionGoals' => [
            'calories' => 2200,
            'proteins' => 135.5,
            'carbs' => 260,
            'fats' => 75.25,
        ],
    ]);

    $response
        ->assertOk()
        ->assertJsonPath('data.nutritionGoals.calories', 2200)
        ->assertJsonPath('data.nutritionGoals.proteins', 135.5)
        ->assertJsonPath('data.nutritionGoals.carbs', 260)
        ->assertJsonPath('data.nutritionGoals.fats', 75.25);

    $this->assertDatabaseHas('users', [
        'id' => $user->id,
        'daily_calorie_goal' => 2200,
        'daily_protein_goal' => 135.5,
        'daily_carbs_goal' => 260,
        'daily_fats_goal' => 75.25,
    ]);
});

it('validates nutrition goals', function () {
    Sanctum::actingAs(User::factory()->create());

    $this->patchJson('/api/auth/me', [
        'nutritionGoals' => [
            'calories' => -1,
            'proteins' => 'abc',
        ],
    ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors([
            'nutritionGoals.calories',
            'nutritionGoals.proteins',
            'nutritionGoals.carbs',
            'nutritionGoals.fats',
        ]);
});
