<?php

declare(strict_types=1);

use App\Models\Branch;
use App\Support\Branch\BranchContext;
use Modules\{{ Module }}\Contracts\{{ Entity }}ServiceInterface;
use Modules\{{ Module }}\DTOs\{{ Entity }}Data;

/**
 * A feature test drives the use case, not the controller. If this passes
 * without an HTTP session, the future mobile app can reach the same behaviour
 * through the same service.
 */
beforeEach(function (): void {
    // Branch-owned models inherit the active branch. Background jobs and tests
    // have no session, so the context is set explicitly — which is the whole
    // point of it not being a global.
    $branch = Branch::factory()->primary()->create();

    app(BranchContext::class)->set($branch->id);
});

it('creates a {{ entity }}', function (): void {
    $service = app({{ Entity }}ServiceInterface::class);

    ${{ entity }} = $service->create(new {{ Entity }}Data(name: 'Example'));

    expect(${{ entity }}->name)->toBe('Example')
        ->and(${{ entity }}->ulid)->not->toBeEmpty()
        ->and(${{ entity }}->branch_id)->not->toBeNull();
});

it('scopes queries to the active branch', function (): void {
    $service = app({{ Entity }}ServiceInterface::class);
    $service->create(new {{ Entity }}Data(name: 'In my branch'));

    $other = Branch::factory()->create();
    app(BranchContext::class)->set($other->id);

    expect(\Modules\{{ Module }}\Models\{{ Entity }}::count())->toBe(0);
});
