<?php

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

uses(RefreshDatabase::class);

it('returns nutrition totals for a day', function () {
    $user = User::factory()->create();
    $otherUser = User::factory()->create();

    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 500,
        'proteins' => 30.50,
        'carbs' => 60.25,
        'fats' => 12.75,
        'eaten_at' => '2026-05-16 08:30:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 250,
        'proteins' => 10.25,
        'carbs' => 20,
        'fats' => 5.50,
        'eaten_at' => '2026-05-16 20:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 900,
        'proteins' => 80,
        'carbs' => 90,
        'fats' => 40,
        'eaten_at' => '2026-05-15 12:00:00',
    ]);
    MealPosts::factory()->for($otherUser, 'user')->create([
        'calories' => 700,
        'proteins' => 60,
        'carbs' => 70,
        'fats' => 30,
        'eaten_at' => '2026-05-16 12:00:00',
    ]);

    Sanctum::actingAs($user);

    $response = $this->getJson('/api/meal-posts/stats?period=day&date=2026-05-16');

    $response
        ->assertOk()
        ->assertJsonPath('data.period', 'day')
        ->assertJsonPath('data.date', '2026-05-16')
        ->assertJsonPath('data.startDate', '2026-05-16')
        ->assertJsonPath('data.endDate', '2026-05-16')
        ->assertJsonPath('data.totals.calories', 750)
        ->assertJsonPath('data.totals.proteins', 40.75)
        ->assertJsonPath('data.totals.carbs', 80.25)
        ->assertJsonPath('data.totals.fats', 18.25)
        ->assertJsonPath('data.totals.mealsCount', 2)
        ->assertJsonCount(1, 'data.days')
        ->assertJsonPath('data.days.0.date', '2026-05-16')
        ->assertJsonPath('data.days.0.totals.calories', 750);
});

it('returns nutrition totals for a week with a daily breakdown', function () {
    $user = User::factory()->create();

    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 400,
        'proteins' => 20,
        'carbs' => 45,
        'fats' => 8,
        'eaten_at' => '2026-05-11 10:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 600,
        'proteins' => 35.50,
        'carbs' => 80.25,
        'fats' => 14.75,
        'eaten_at' => '2026-05-16 12:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 300,
        'proteins' => 18,
        'carbs' => 25,
        'fats' => 11,
        'eaten_at' => '2026-05-17 21:30:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'calories' => 999,
        'proteins' => 99,
        'carbs' => 99,
        'fats' => 99,
        'eaten_at' => '2026-05-18 08:00:00',
    ]);

    Sanctum::actingAs($user);

    $response = $this->getJson('/api/meal-posts/stats?period=week&date=2026-05-16');

    $response
        ->assertOk()
        ->assertJsonPath('data.period', 'week')
        ->assertJsonPath('data.startDate', '2026-05-11')
        ->assertJsonPath('data.endDate', '2026-05-17')
        ->assertJsonPath('data.totals.calories', 1300)
        ->assertJsonPath('data.totals.proteins', 73.5)
        ->assertJsonPath('data.totals.carbs', 150.25)
        ->assertJsonPath('data.totals.fats', 33.75)
        ->assertJsonPath('data.totals.mealsCount', 3)
        ->assertJsonCount(7, 'data.days')
        ->assertJsonPath('data.days.0.date', '2026-05-11')
        ->assertJsonPath('data.days.0.totals.calories', 400)
        ->assertJsonPath('data.days.5.date', '2026-05-16')
        ->assertJsonPath('data.days.5.totals.calories', 600)
        ->assertJsonPath('data.days.6.date', '2026-05-17')
        ->assertJsonPath('data.days.6.totals.calories', 300);
});

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

    $response = $this->getJson('/api/meal-posts/stats?period=month&date=16-05-2026');

    $response
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['period', 'date']);
});
