# Wiring the Phase 2 overlay

Prereq: Phase 1 installed and its tests green.

## 1. Copy files (preserve paths), then MERGE routes/web.php into the Phase 1 group.

## 2. Register seeders — database/seeders/DatabaseSeeder.php
```php
$this->call(\Database\Seeders\IntegrationRegistrySeeder::class);
// DemoTenantSeeder (Phase 1) already runs; order: permissions -> integrations -> tenants
```

## 3. Add the scheduled incremental sync — routes/console.php (or bootstrap/app.php withSchedule)
```php
use App\Integrations\Models\IntegrationAccount;
use App\Sync\Jobs\RunSyncJob;
use Illuminate\Support\Facades\Schedule;

Schedule::call(function () {
    IntegrationAccount::withoutOrganizationScope()
        ->where('sync_frequency', '!=', 'manual')
        ->where(fn ($q) => $q->whereNull('next_sync_at')->orWhere('next_sync_at', '<=', now()))
        ->each(function ($account) {
            foreach (($account->selected_entities ?? ['properties']) as $resource) {
                RunSyncJob::dispatch($account->id, $resource);
            }
        });
})->everyFiveMinutes()->name('dispatch-due-syncs')->withoutOverlapping();
```

## 4. Queue + workers
Set `QUEUE_CONNECTION=redis` (or `database` on cPanel without Redis). Run a worker:
`php artisan queue:work --tries=5`. Supervisor config comes in Phase 6.

## 5. Run
```bash
php artisan migrate            # adds Phase 2 tables
php artisan db:seed            # registers providers
php artisan test --filter='AdapterContract|SyncIdempotency|CredentialEncryption|IntegrationTenantIsolation'
```
