<?php

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

uses(RefreshDatabase::class);

it('returns meal post days for a month', function () {
    $user = User::factory()->create();
    $otherUser = User::factory()->create();

    MealPosts::factory()->for($user, 'user')->create([
        'eaten_at' => '2026-02-04 08:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'eaten_at' => '2026-02-04 20:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'eaten_at' => '2026-02-12 12:00:00',
    ]);
    MealPosts::factory()->for($user, 'user')->create([
        'eaten_at' => '2026-03-01 12:00:00',
    ]);
    MealPosts::factory()->for($otherUser, 'user')->create([
        'eaten_at' => '2026-02-12 12:00:00',
    ]);

    Sanctum::actingAs($user);

    $response = $this->getJson('/api/meal-posts/calendar?month=2026-02');

    $response
        ->assertOk()
        ->assertJsonPath('data.month', '2026-02')
        ->assertJsonPath('data.startDate', '2026-02-01')
        ->assertJsonPath('data.endDate', '2026-02-28')
        ->assertJsonCount(2, 'data.days')
        ->assertJsonPath('data.days.0.date', '2026-02-04')
        ->assertJsonPath('data.days.0.mealsCount', 2)
        ->assertJsonPath('data.days.1.date', '2026-02-12')
        ->assertJsonPath('data.days.1.mealsCount', 1);
});

it('validates the requested calendar month', function () {
    Sanctum::actingAs(User::factory()->create());

    $this
        ->getJson('/api/meal-posts/calendar?month=02-2026')
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['month']);
});
