<?php

namespace Database\Seeders;

use App\Models\Solicitor\Solicitor;
use App\Models\WorkboardCategory;
use Illuminate\Database\Seeder;

class InboxWorkboardCategorySeeder extends Seeder
{
    /**
     * Ensure the two standard inbox workboard categories exist for every solicitor.
     * Safe to re-run (uses firstOrCreate).
     */
    public function run(): void
    {
        $categories = [
            [
                'slug'        => 'dsar-evidence',
                'name'        => 'DSAR Evidence',
                'description' => 'Inbound emails classified as DSAR responses that have been confidently matched to an open DSAR.',
            ],
            [
                'slug'        => 'dsar-needs-research',
                'name'        => 'DSAR Needs Research',
                'description' => 'Inbound DSAR responses that were matched to an open DSAR, but the institution\'s reply requires further investigation — e.g. they have no record of the client, say it was sent to the wrong place, claim not to be the data controller, or have formally rejected the request.',
            ],
            [
                'slug'        => 'unknown-inbox',
                'name'        => 'Unknown Inbox',
                'description' => 'Inbound emails classified as DSAR responses where the sending institution could not be identified at all — require manual review.',
            ],
        ];

        $solicitors = Solicitor::all();

        foreach ($solicitors as $solicitor) {
            foreach ($categories as $cat) {
                $exists = WorkboardCategory::withoutGlobalScopes()
                    ->where('solicitor_id', $solicitor->id)
                    ->where('slug', $cat['slug'])
                    ->exists();

                if (!$exists) {
                    WorkboardCategory::withoutGlobalScopes()->create([
                        'solicitor_id' => $solicitor->id,
                        'name'         => $cat['name'],
                        'slug'         => $cat['slug'],
                        'description'  => $cat['description'],
                    ]);

                    $this->command->line("  Created '{$cat['name']}' for solicitor #{$solicitor->id} ({$solicitor->name})");
                } else {
                    $this->command->line("  '{$cat['name']}' already exists for solicitor #{$solicitor->id} — skipped");
                }
            }
        }
    }
}
