<?php

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

test('viewer can read the basic branch list', function () {
    Branch::factory()->count(3)->create();
    $viewer = User::factory()->branchViewer()->create();

    $response = $this->actingAs($viewer)->getJson('/api/v1/branches?fields=basic');

    $response->assertOk();
    $row = $response->json('data.0');
    expect($row)->toHaveKeys(['id', 'name', 'code']);
    expect($row)->not->toHaveKey('email');
});

test('branch list is not restricted to the viewing user\'s own branch', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    $branchAdminA = User::factory()->branchAdmin()->create(['branch_id' => $branchA->id]);

    $response = $this->actingAs($branchAdminA)->getJson('/api/v1/branches');

    $response->assertOk();
    $ids = collect($response->json('data'))->pluck('id');
    expect($ids)->toContain($branchA->id, $branchB->id);
});

test('branch detail includes a cached members_count', function () {
    $branch = Branch::factory()->create();
    Member::factory()->count(3)->create(['branch_id' => $branch->id]);
    $superAdmin = User::factory()->superAdmin()->create();

    $response = $this->actingAs($superAdmin)->getJson("/api/v1/branches/{$branch->id}");

    $response->assertOk()->assertJsonPath('data.members_count', 3);
});

test('super_admin can create, update, and delete a branch', function () {
    $superAdmin = User::factory()->superAdmin()->create();
    $region = Region::factory()->create();

    $create = $this->actingAs($superAdmin)->postJson('/api/v1/branches', [
        'region_id' => $region->id,
        'name' => 'New Branch',
        'code' => 'NB-001',
    ]);
    $create->assertCreated();
    $branchId = $create->json('data.id');

    $update = $this->actingAs($superAdmin)->putJson("/api/v1/branches/{$branchId}", [
        'region_id' => $region->id,
        'name' => 'Renamed Branch',
        'code' => 'NB-001',
    ]);
    $update->assertOk()->assertJsonPath('data.name', 'Renamed Branch');

    $delete = $this->actingAs($superAdmin)->deleteJson("/api/v1/branches/{$branchId}");
    $delete->assertStatus(204);
});

test('region_admin can create a branch only within their own region', function () {
    $ownRegion = Region::factory()->create();
    $otherRegion = Region::factory()->create();
    $regionAdmin = User::factory()->regionAdmin()->create(['region_id' => $ownRegion->id]);

    $this->actingAs($regionAdmin)->postJson('/api/v1/branches', [
        'region_id' => $ownRegion->id,
        'name' => 'In Region Branch',
        'code' => 'IR-001',
    ])->assertCreated();

    $this->actingAs($regionAdmin)->postJson('/api/v1/branches', [
        'region_id' => $otherRegion->id,
        'name' => 'Out Of Region Branch',
        'code' => 'OOR-001',
    ])->assertStatus(403);
});

test('region_admin cannot move a branch to another region', function () {
    $ownRegion = Region::factory()->create();
    $otherRegion = Region::factory()->create();
    $regionAdmin = User::factory()->regionAdmin()->create(['region_id' => $ownRegion->id]);
    $branch = Branch::factory()->create(['region_id' => $ownRegion->id]);

    $this->actingAs($regionAdmin)->putJson("/api/v1/branches/{$branch->id}", [
        'region_id' => $otherRegion->id,
        'name' => $branch->name,
        'code' => $branch->code,
    ])->assertStatus(422)->assertJsonValidationErrors('region_id');
});

test('branch_admin cannot create or update branches', function () {
    $branch = Branch::factory()->create();
    $branchAdmin = User::factory()->branchAdmin()->create(['branch_id' => $branch->id]);

    $this->actingAs($branchAdmin)->postJson('/api/v1/branches', [
        'region_id' => $branch->region_id,
        'name' => 'Sneaky Branch',
        'code' => 'SN-001',
    ])->assertStatus(403);

    $this->actingAs($branchAdmin)->putJson("/api/v1/branches/{$branch->id}", [
        'region_id' => $branch->region_id,
        'name' => 'Renamed',
        'code' => $branch->code,
    ])->assertStatus(403);
});

test('deleting a branch with members is blocked with 409', function () {
    $superAdmin = User::factory()->superAdmin()->create();
    $branch = Branch::factory()->create();
    Member::factory()->create(['branch_id' => $branch->id]);

    $this->actingAs($superAdmin)->deleteJson("/api/v1/branches/{$branch->id}")->assertStatus(409);
});

test('unauthenticated request to list branches is rejected', function () {
    $this->getJson('/api/v1/branches')->assertStatus(401);
});
