<?php

function decodeJsonFile(string $path): array
{
    $decoded = json_decode(file_get_contents($path), true);

    expect($decoded)->toBeArray();

    return $decoded;
}

it('defines the official root workspace command model', function () {
    $package = decodeJsonFile(repo_path('package.json'));

    expect($package['packageManager'] ?? null)->toBe('pnpm@10.33.0');
    expect($package['scripts'] ?? [])
        ->toHaveKeys([
            'dev',
            'dev:platform',
            'dev:website',
            'build:platform',
            'build:website',
        ]);
    expect($package['scripts']['dev'] ?? null)->toContain('corepack pnpm dev:website');
    expect($package['scripts']['dev:platform'] ?? null)->toBe('./scripts/platform-sail up -d');
    expect($package['scripts']['dev:website'] ?? null)->toContain('WEBSITE_PORT=${WEBSITE_PORT:-4321}');
    expect($package['scripts']['build:platform'] ?? null)->toContain('@tenantatlas/platform build');
    expect($package['scripts']['build:website'] ?? null)->toContain('@tenantatlas/website build');
});

it('binds the repo to apps workspaces and removes the legacy npm lockfile', function () {
    $workspaceDefinition = file_get_contents(repo_path('pnpm-workspace.yaml'));

    expect($workspaceDefinition)->toContain('apps/*');
    expect(file_exists(repo_path('apps/platform/package-lock.json')))->toBeFalse();
});

it('keeps platform and website manifests aligned to the workspace boundary', function () {
    $platformPackage = decodeJsonFile(repo_path('apps/platform/package.json'));
    $websitePackage = decodeJsonFile(repo_path('apps/website/package.json'));

    expect($platformPackage['name'] ?? null)->toBe('@tenantatlas/platform');
    expect($platformPackage['scripts']['sail:dev'] ?? null)->toBe('./vendor/bin/sail pnpm dev');
    expect($platformPackage['scripts']['sail:build'] ?? null)->toBe('./vendor/bin/sail pnpm build');

    expect($websitePackage['name'] ?? null)->toBe('@tenantatlas/website');
    expect($websitePackage['scripts'] ?? [])
        ->toHaveKeys(['dev', 'build', 'preview']);
    expect($websitePackage['scripts']['dev'] ?? null)->toContain('${WEBSITE_PORT:-4321}');
});
