<?php

namespace Database\Seeders;

use App\Models\Policy;
use App\Models\Tenant;
use Illuminate\Database\Seeder;

class PoliciesSeeder extends Seeder
{
    public function run(): void
    {
        $tenant = Tenant::firstOrCreate([
            'tenant_id' => env('INTUNE_TENANT_ID', 'local-tenant'),
        ], [
            'name' => 'Default Tenant',
            'domain' => null,
            'metadata' => [],
        ]);

        $supported = config('tenantpilot.supported_policy_types', []);
        $now = now();

        foreach ($supported as $index => $type) {
            $policyType = $type['type'];
            $platform = $type['platform'] ?? null;
            $externalId = 'seed-'.$policyType.'-'.($platform ?? 'any').'-'.$index;

            Policy::updateOrCreate(
                [
                    'tenant_id' => $tenant->id,
                    'external_id' => $externalId,
                    'policy_type' => $policyType,
                ],
                [
                    'display_name' => $type['label'] ?? ucfirst($policyType),
                    'platform' => $platform,
                    'last_synced_at' => $now,
                    'metadata' => ['seeded' => true],
                ]
            );
        }
    }
}
