# Export Real Companies Job - Usage Guide

## Overview
The `ExportRealCompaniesJson` job automatically exports all real (reliable) companies to a JSON file at `public_html/multicart/data/domains.json`.

## Job Location
`app/Jobs/ExportRealCompaniesJson.php`

## Usage Examples

### 1. Dispatch After Company Updates
When a company is created or updated, automatically export the companies list:

```php
use App\Jobs\ExportRealCompaniesJson;

// After company update/create
dispatch(new ExportRealCompaniesJson())->afterResponse();
```

### 2. In Company Controller
Add to `CompanyController@update` method:

```php
public function update($id)
{
    // ... existing update code ...
    
    if ($result) {
        // Dispatch job to export companies
        dispatch(new \App\Jobs\ExportRealCompaniesJson())->afterResponse();
        
        session()->flash('success', trans('saas::app.tenant.registration.company-updated'));
    }
    
    return redirect()->back();
}
```

### 3. In Middleware (Like UpdateHomepageJsonCache)
Add to `Locale.php` middleware's `terminate` method:

```php
public function terminate($request, $response)
{
    if (env('APP_URL') === "https://store.markatty.com" && !$request->isMethod('GET')) {
        // Update homepage cache
        dispatch(new \App\Jobs\UpdateHomepageJsonCache())->afterResponse();
        
        // Export companies when company-related routes are accessed
        $currentRouteName = $request->route()->getName();
        if (str_contains($currentRouteName, 'company') || str_contains($currentRouteName, 'tenant')) {
            dispatch(new \App\Jobs\ExportRealCompaniesJson())->afterResponse();
        }
    }
}
```

### 4. Schedule Regular Exports
In `app/Console/Kernel.php`, schedule the job to run periodically:

```php
protected function schedule(Schedule $schedule)
{
    // Export companies every hour
    $schedule->job(new \App\Jobs\ExportRealCompaniesJson())->hourly();
    
    // Or export companies every day at 2 AM
    $schedule->job(new \App\Jobs\ExportRealCompaniesJson())->dailyAt('02:00');
}
```

### 5. Manual Dispatch in Any Controller
```php
use App\Jobs\ExportRealCompaniesJson;

// Dispatch immediately
ExportRealCompaniesJson::dispatch();

// Dispatch after response
dispatch(new ExportRealCompaniesJson())->afterResponse();

// Dispatch with delay (5 minutes)
ExportRealCompaniesJson::dispatch()->delay(now()->addMinutes(5));
```

### 6. In Event Listeners
Create an event listener for company changes:

```php
namespace App\Listeners;

use App\Jobs\ExportRealCompaniesJson;

class CompanyUpdatedListener
{
    public function handle($event)
    {
        dispatch(new ExportRealCompaniesJson())->afterResponse();
    }
}
```

## Features
- ✅ Only runs on production (`store.markatty.com`)
- ✅ Async execution (doesn't block the response)
- ✅ 120-second lock to prevent duplicate exports
- ✅ Automatic timeout handling (5 seconds)
- ✅ SSL verification disabled for local testing

## Output
The job creates/updates: `public_html/multicart/data/domains.json`

Format:
```json
{
    "domain.com": {
        "id": 123,
        "domain": "domain.com",
        "channel_id": 456,
        "default_locale": "en",
        "branch_id": 789,
        "layout": 1
    }
}
```

## Notes
- The job uses cURL to call `/script/export-real-companies` endpoint
- It prioritizes `cname` (custom domain) over regular `domain`
- All domains are trimmed to remove whitespace and newlines
- Only exports companies where `reliable = 1`
