<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\PlatformUser;
use App\Models\SupportAccessGrant;
use App\Models\Workspace;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<SupportAccessGrant>
 */
class SupportAccessGrantFactory extends Factory
{
    protected $model = SupportAccessGrant::class;

    public function definition(): array
    {
        $requestedAt = now()->subMinutes(5);

        return [
            'workspace_id' => Workspace::factory(),
            'requested_by_platform_user_id' => PlatformUser::factory(),
            'approved_by_user_id' => null,
            'scope' => SupportAccessGrant::SCOPE_AUDIT_VIEW,
            'status' => SupportAccessGrant::STATUS_ACTIVE,
            'approval_mode' => SupportAccessGrant::APPROVAL_MODE_AUTO,
            'reason' => 'Review audit history for a support case',
            'waiver_reason' => null,
            'ttl_minutes' => 60,
            'requested_at' => $requestedAt,
            'approved_at' => null,
            'starts_at' => $requestedAt,
            'expires_at' => now()->addMinutes(55),
            'ended_at' => null,
            'denied_at' => null,
        ];
    }

    public function pendingRecovery(): static
    {
        return $this->state(fn (array $attributes): array => [
            'scope' => SupportAccessGrant::SCOPE_WORKSPACE_RECOVERY,
            'status' => SupportAccessGrant::STATUS_REQUESTED,
            'approval_mode' => SupportAccessGrant::APPROVAL_MODE_OWNER_REQUIRED,
            'approved_at' => null,
            'starts_at' => null,
            'expires_at' => null,
            'ended_at' => null,
            'denied_at' => null,
        ]);
    }

    public function activeRecovery(): static
    {
        return $this->state(fn (array $attributes): array => [
            'scope' => SupportAccessGrant::SCOPE_WORKSPACE_RECOVERY,
            'status' => SupportAccessGrant::STATUS_ACTIVE,
            'approval_mode' => SupportAccessGrant::APPROVAL_MODE_OWNER_REQUIRED,
            'approved_at' => now()->subMinutes(4),
            'starts_at' => now()->subMinutes(4),
            'expires_at' => now()->addMinutes(56),
        ]);
    }

    public function expired(): static
    {
        return $this->state(fn (array $attributes): array => [
            'status' => SupportAccessGrant::STATUS_ACTIVE,
            'starts_at' => now()->subHours(2),
            'expires_at' => now()->subMinute(),
        ]);
    }
}
