# 🚀 X Publishing Quick Start Guide

## 5 Minute Setup

### 1. Verify Credentials (1 min)
```bash
php artisan x:test-oauth2 --validate
```

### 2. Test Connection (1 min)
```bash
php artisan x:test-oauth2 --user-info
```

### 3. Refresh Token (1 min)
```bash
php artisan x:test-oauth2 --refresh
```

### 4. Post Test Tweet (1 min)
```bash
php artisan x:test-oauth2 --tweet="Testing OAuth 2.0 from Bokeplah 🎥"
```

### 5. Monitor Logs (1 min)
```bash
tail -f storage/logs/laravel.log | grep "X\|OAuth"
```

---

## Common Use Cases

### 📝 Post Simple Tweet
```php
$xService = app(\App\Services\XPublishService::class);
$tweetId = $xService->postSimpleTweet('Hello World! 🚀');
```

### 🖼️ Post Tweet with Image
```php
$xService = app(\App\Services\XPublishService::class);

// Upload media
$mediaId = $xService->uploadMedia('https://play.bokeplah.me/storage/publish/image.jpg');

// Post with media
$tweetId = $xService->postTweet('Check this out! 🎥', $mediaId);
```

### ✅ Validate Setup
```php
$xService = app(\App\Services\XPublishService::class);
$status = $xService->validateCredentials();

if (!$status['valid']) {
    dd($status['errors']);
}
```

### 🔄 Manual Token Refresh
```php
$xService = app(\App\Services\XPublishService::class);
if ($xService->refreshAccessTokenOAuth2()) {
    echo "Token refreshed!";
}
```

### 👤 Get User Info
```php
$xService = app(\App\Services\XPublishService::class);
$user = $xService->getUserInfo();
echo "Posting as: @{$user['username']}";
```

---

## Queue Publishing (Recommended)

### Auto-publish via Job
```php
use App\Jobs\XPublishJob;
use App\Models\PublishLog;

$publishLog = PublishLog::create([
    'video_id' => $video->id,
    'platform' => 'x',
    'publish_title' => 'Video Title',
    'status' => 'pending',
]);

// Queue job
dispatch(new XPublishJob($publishLog));
```

### Job Monitoring
```bash
# Check job status
php artisan queue:monitor

# Process jobs
php artisan queue:work

# View job logs
tail -f storage/logs/laravel.log
```

---

## Debugging

### Check Auth Status
```bash
php artisan tinker
```

```php
$x = app(\App\Services\XPublishService::class);
dd($x->getAuthInfo());
```

### Check Logs
```bash
# Real-time logs
tail -f storage/logs/laravel.log | grep "X\|OAuth\|tweet"

# Search for errors
grep -i "error" storage/logs/laravel.log | tail -20

# Search for specific date
grep "2026-06-12" storage/logs/laravel.log
```

### Manual Testing
```bash
# Single tweet
php artisan x:test-oauth2 --tweet="Test from CLI"

# With refresh
php artisan x:test-oauth2 --refresh --user-info

# Full validation
php artisan x:test-oauth2 --validate
```

---

## Environment Setup

### Minimal `.env`
```env
X_API_KEY=your_api_key
X_API_SECRET=your_api_secret
X_ACCESS_TOKEN=your_access_token
X_REFRESH_TOKEN=your_refresh_token
```

### Full `.env` (Optional)
```env
X_API_KEY=m0HLWW06XT8WJrLNhEjrVwuSu
X_API_SECRET=9JvRI90gpUrH9NF2xkQeBx6RsHxXe4UcHiEIFMQJyjbPP27oKI
X_ACCESS_TOKEN=1885551225606930432-aVcEwGMumCRYKncmvTkdi5fo3lyXEl
X_ACCESS_TOKEN_SECRET=odbegOJLQVuerlkrS9O25zSYGQ0CjLbhoe0GtQ3vrtaGT
X_REFRESH_TOKEN=Nk5QelBZQlNrN2lBQ0pISjdZQWtPWE1JbzFVMGtyc1B6LVdKN29lQUxta1huOjE3ODExODE2MzE0MTY6MToxOnJ0OjE
X_BEARER_TOKEN=AAAAAAAAAAAAAAAAAAAAAPiV7AEAAAAAVoTk1MJOWfpIX2jFxDIOAhl9LJk%3DHoJWT33fcxz0DjYGxmJX8VQjRfgXw1QZCI5mbzFODPAYExgZfr
```

---

## Error Handling

### Auto-Retry on 401
```
Request fails with 401
  ↓
Service automatically refreshes token
  ↓
Retries request
  ↓
Success ✅
```

### Manual Recovery
```php
try {
    $xService->postSimpleTweet('Tweet');
} catch (\Throwable $e) {
    if (str_contains($e->getMessage(), '401')) {
        $xService->refreshAccessTokenOAuth2();
        // Retry...
    }
}
```

---

## Performance Tips

1. **Use Queue Jobs** untuk batch publishing
   ```php
   dispatch(new XPublishJob($publishLog))->delay(now()->addMinutes(5));
   ```

2. **Refresh Token During Off-Peak**
   ```bash
   0 2 * * * php artisan x:test-oauth2 --refresh
   ```

3. **Monitor Rate Limits**
   - 300 tweets / 15 minutes
   - 15 media uploads / 15 minutes

4. **Cache User Info**
   ```php
   $user = Cache::remember('x.user_info', 3600, function () {
       return app(\App\Services\XPublishService::class)->getUserInfo();
   });
   ```

---

## Support

### Check Documentation
- [X_OAUTH2_SETUP.md](X_OAUTH2_SETUP.md) - Detailed setup
- [X_OAUTH2_UPDATE_SUMMARY.md](X_OAUTH2_UPDATE_SUMMARY.md) - Technical details

### Test Files
- [test_x_oauth2_setup.php](test_x_oauth2_setup.php) - Standalone test
- [app/Console/Commands/TestXOAuth2Command.php](app/Console/Commands/TestXOAuth2Command.php) - Artisan command

### API Docs
- [Twitter API v2 Documentation](https://developer.twitter.com/en/docs/twitter-api)
- [OAuth 2.0 Guide](https://developer.twitter.com/en/docs/authentication/oauth-2-0)

---

## Checklist

- [ ] Run `php artisan x:test-oauth2 --validate`
- [ ] Verify credentials in output
- [ ] Test with `--tweet` flag
- [ ] Check logs for success messages
- [ ] Post first real video
- [ ] Monitor for errors
- [ ] Setup production monitoring

---

Good luck with X publishing! 🚀

For issues: Check logs and run `php artisan x:test-oauth2` for diagnostics.
