<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\StoredReport;
use App\Models\ManagedEnvironment;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<StoredReport>
 */
class StoredReportFactory extends Factory
{
    protected $model = StoredReport::class;

    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'managed_environment_id' => ManagedEnvironment::factory(),
            'report_type' => StoredReport::REPORT_TYPE_PERMISSION_POSTURE,
            'payload' => [
                'posture_score' => 86,
                'required_count' => 14,
                'granted_count' => 12,
                'checked_at' => now()->toIso8601String(),
                'permissions' => [
                    [
                        'key' => 'DeviceManagementConfiguration.ReadWrite.All',
                        'type' => 'application',
                        'status' => 'granted',
                        'features' => ['policy-sync', 'backup', 'restore'],
                    ],
                    [
                        'key' => 'DeviceManagementApps.ReadWrite.All',
                        'type' => 'application',
                        'status' => 'missing',
                        'features' => ['policy-sync', 'backup'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function permissionPosture(array $payload = []): self
    {
        return $this->state(fn (): array => [
            'report_type' => StoredReport::REPORT_TYPE_PERMISSION_POSTURE,
            'payload' => array_replace_recursive([
                'posture_score' => 86,
                'required_count' => 14,
                'granted_count' => 12,
                'checked_at' => now()->toIso8601String(),
                'permissions' => [
                    [
                        'key' => 'DeviceManagementConfiguration.ReadWrite.All',
                        'type' => 'application',
                        'status' => 'granted',
                        'features' => ['policy-sync', 'backup', 'restore'],
                    ],
                    [
                        'key' => 'DeviceManagementApps.ReadWrite.All',
                        'type' => 'application',
                        'status' => 'missing',
                        'features' => ['policy-sync', 'backup'],
                    ],
                ],
            ], $payload),
        ]);
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function entraAdminRoles(array $payload = []): self
    {
        return $this->state(fn (): array => [
            'report_type' => StoredReport::REPORT_TYPE_ENTRA_ADMIN_ROLES,
            'payload' => array_replace_recursive([
                'provider_key' => 'microsoft',
                'domain' => 'entra.admin_roles',
                'measured_at' => now()->toIso8601String(),
                'role_definitions' => [],
                'role_assignments' => [],
                'totals' => [
                    'roles_total' => 8,
                    'assignments_total' => 12,
                    'high_privilege_assignments' => 5,
                ],
                'high_privilege' => [
                    [
                        'role_display_name' => 'Global Administrator',
                        'role_template_id' => '62e90394-69f5-4237-9190-012177145e10',
                        'principal_id' => 'user-1',
                        'principal_display_name' => 'Admin User',
                        'assignment_scope' => '/',
                        'severity' => 'critical',
                    ],
                ],
            ], $payload),
        ]);
    }
}
