# This Batch — Wiring Notes

## 1. Register the service provider

Laravel 12 registers providers in `bootstrap/providers.php` (not `config/app.php`). Add:

```php
<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\AIServiceProvider::class,
];
```

## 2. Run migrations + seeder

```bash
php artisan migrate
php artisan db:seed --class=Database\\Seeders\\AiProviderSettingSeeder
```

This creates 6 rows in `ai_provider_settings` (openai, anthropic, gemini, deepseek, mistral, ollama), all `is_active=false, is_configured=false`.

## 3. Activate a provider

Until the Admin > AI Settings UI is built (next batch), you can activate one manually:

```php
$setting = App\Models\AiProviderSetting::where('provider', 'anthropic')->first();
$setting->update([
    'api_key' => 'sk-ant-...',      // stored encrypted via the model cast
    'model_name' => 'claude-sonnet-5',
    'is_configured' => true,
    'is_active' => true,             // observer auto-deactivates all other rows
]);
```

## 4. Use it anywhere in the app

```php
use App\Contracts\AI\AIProviderInterface;

final class RewriteArticleAction
{
    public function __construct(private readonly AIProviderInterface $ai) {}

    public function execute(News $news): void
    {
        $result = $this->ai->rewriteArticle($news->original_title, $news->original_body);

        $news->update([
            'title' => $result->title,
            'body' => $result->body,
            'excerpt' => $result->excerpt,
            'ai_processed' => true,
            'ai_processed_at' => now(),
        ]);
    }
}
```

`RewriteArticleAction` never references OpenAI, Anthropic, or any vendor — it depends only on `AIProviderInterface`. Swapping the active provider in the admin panel changes what runs here with zero code changes, exactly as required.

## What's included in this batch

- `ARCHITECTURE.md` — full folder structure
- `DATABASE_SCHEMA.md` — full ~90-table plan, with Sections E (partial)/G/H/J detailed
- 19 migration files — core (countries, languages, leagues, teams, players), RSS aggregator (sources, logs, news, translations, tags), AI Content Engine (provider settings, generation logs, embeddings, similarity scores, translation queue, internal links, prompt templates), and the sports-data API abstraction (mirrors the AI provider pattern)
- Full provider-agnostic AI architecture:
  - `AIProviderInterface` (9 capability methods, exactly as specified)
  - `AbstractAIProvider` (shared prompt templates, JSON parsing, audit logging)
  - `OpenAICompatibleProvider` (shared OpenAI-wire-format base for DeepSeek/Mistral/Ollama)
  - 6 concrete providers: OpenAI, Anthropic, Gemini, DeepSeek, Mistral, Ollama
  - `AIProviderFactory` + `AIManager` (with automatic fallback chain by priority)
  - `AiProviderSettingObserver` (enforces exactly-one-active-provider)
  - `AIServiceProvider` (the single binding point: `AIProviderInterface` → `AIManager`)
  - 9 DTOs, 2 enums, config file, seeder

## Not yet included (next batches, per your START ORDER)

- Full Models layer for every table (League, Team, Player, Match, etc. — only `News`, `AiProviderSetting`, `AiGenerationLog` exist so far, needed by this batch)
- Repositories, remaining Services (RSS reader, similarity detection, image downloader)
- API layer (Controllers, Requests, Resources)
- Authentication (Sanctum, Spatie Permission)
- Admin Panel (incl. the AI Settings screen — provider dropdown, credential form, "Test Connection" button wired to `AIManager::using()`)
- Frontend (Blade/Livewire)
- Live Scores, remaining Statistics tables
- SEO, Scheduler, Queue System, Testing, Deployment guide
