<?php

use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
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.locale', 'en')
        ->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',
        ]);
});

it('returns profile preferences with the authenticated user', function () {
    $user = User::factory()->create([
        'physical_activity_level' => PhysicalActivityLevel::VERY_ACTIVE->value,
        'weight_goal' => WeightGoal::GAIN_WEIGHT->value,
        'pace_preference' => PacePreference::FAST->value,
    ]);

    Sanctum::actingAs($user);

    $this->getJson('/api/auth/me')
        ->assertOk()
        ->assertJsonPath('data.physicalActivityLevel', PhysicalActivityLevel::VERY_ACTIVE->value)
        ->assertJsonPath('data.weightGoal', WeightGoal::GAIN_WEIGHT->value)
        ->assertJsonPath('data.pacePreference', PacePreference::FAST->value);
});

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

    Sanctum::actingAs($user);

    $this->patchJson('/api/auth/me', [
        'physicalActivityLevel' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
        'weightGoal' => WeightGoal::MAINTAIN_WEIGHT->value,
        'pacePreference' => PacePreference::NORMAL->value,
        'sex' => UserSex::MAN->value,
        'dateOfBirth' => '2003-04-24',
        'locale' => 'fr-FR',
    ])
        ->assertOk()
        ->assertJsonPath('data.physicalActivityLevel', PhysicalActivityLevel::MODERATELY_ACTIVE->value)
        ->assertJsonPath('data.weightGoal', WeightGoal::MAINTAIN_WEIGHT->value)
        ->assertJsonPath('data.pacePreference', PacePreference::NORMAL->value)
        ->assertJsonPath('data.sex', UserSex::MAN->value)
        ->assertJsonPath('data.locale', 'fr')
        ->assertJsonPath('data.dateOfBirth', '2003-04-24');

    $this->assertDatabaseHas('users', [
        'id' => $user->id,
        'locale' => 'fr',
        'physical_activity_level' => PhysicalActivityLevel::MODERATELY_ACTIVE->value,
        'weight_goal' => WeightGoal::MAINTAIN_WEIGHT->value,
        'pace_preference' => PacePreference::NORMAL->value,
        'sex' => UserSex::MAN->value,
    ]);

    expect($user->fresh()->date_of_birth?->toDateString())->toBe('2003-04-24');
});

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

    $this->patchJson('/api/auth/me', [
        'physicalActivityLevel' => 'daily',
        'weightGoal' => 'bulk',
        'pacePreference' => 'urgent',
        'sex' => 'x',
        'locale' => 'es',
        'dateOfBirth' => 'tomorrow',
    ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors([
            'physicalActivityLevel',
            'weightGoal',
            'pacePreference',
            'sex',
            'locale',
            'dateOfBirth',
        ]);
});
