<?php

use App\Models\Branch;
use App\Models\Member;
use App\Models\User;

test('the birthday report filters by month', function () {
    $branch = Branch::factory()->create();
    $inMarch = Member::factory()->create(['branch_id' => $branch->id, 'date_of_birth' => '1990-03-15']);
    Member::factory()->create(['branch_id' => $branch->id, 'date_of_birth' => '1990-04-15']);
    $admin = User::factory()->branchAdmin()->create(['branch_id' => $branch->id]);

    $response = $this->actingAs($admin)->getJson('/api/v1/reports/birthdays?month=3');

    $response->assertOk();
    $ids = collect($response->json('data'))->pluck('id');
    expect($ids)->toContain($inMarch->id)->toHaveCount(1);
});

test('month boundaries are respected: the last day of one month is excluded from the next', function () {
    $branch = Branch::factory()->create();
    $endOfJanuary = Member::factory()->create(['branch_id' => $branch->id, 'date_of_birth' => '1985-01-31']);
    $startOfFebruary = Member::factory()->create(['branch_id' => $branch->id, 'date_of_birth' => '1985-02-01']);
    $admin = User::factory()->branchAdmin()->create(['branch_id' => $branch->id]);

    $januaryResponse = $this->actingAs($admin)->getJson('/api/v1/reports/birthdays?month=1');
    $februaryResponse = $this->actingAs($admin)->getJson('/api/v1/reports/birthdays?month=2');

    expect(collect($januaryResponse->json('data'))->pluck('id'))->toContain($endOfJanuary->id)->not->toContain($startOfFebruary->id);
    expect(collect($februaryResponse->json('data'))->pluck('id'))->toContain($startOfFebruary->id)->not->toContain($endOfJanuary->id);
});

test('a branch_admin only sees birthdays within their own branch', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    Member::factory()->create(['branch_id' => $branchA->id, 'date_of_birth' => '1990-05-10']);
    Member::factory()->create(['branch_id' => $branchB->id, 'date_of_birth' => '1990-05-11']);
    $adminA = User::factory()->branchAdmin()->create(['branch_id' => $branchA->id]);

    $response = $this->actingAs($adminA)->getJson('/api/v1/reports/birthdays?month=5');

    expect($response->json('data'))->toHaveCount(1);
});

test('a super_admin can further narrow the birthday report to a single branch', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    Member::factory()->create(['branch_id' => $branchA->id, 'date_of_birth' => '1990-06-10']);
    Member::factory()->create(['branch_id' => $branchB->id, 'date_of_birth' => '1990-06-11']);
    $superAdmin = User::factory()->superAdmin()->create();

    $response = $this->actingAs($superAdmin)->getJson("/api/v1/reports/birthdays?month=6&branch_id={$branchA->id}");

    expect($response->json('data'))->toHaveCount(1);
});

test('month is required', function () {
    $branch = Branch::factory()->create();
    $admin = User::factory()->branchAdmin()->create(['branch_id' => $branch->id]);

    $this->actingAs($admin)->getJson('/api/v1/reports/birthdays')->assertStatus(422);
});

test('unauthenticated request to the birthday report is rejected', function () {
    $this->getJson('/api/v1/reports/birthdays?month=1')->assertStatus(401);
});
