<?php

namespace Technical\Integrations\Database\Factories;

use Functional\Users\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Technical\Integrations\Enums\IntegrationStatus;
use Technical\Integrations\Models\Integration;

/**
 * @extends Factory<Integration>
 */
class IntegrationFactory extends Factory
{
    protected $model = Integration::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $connectedAt = fake()->dateTimeBetween('-6 months', 'now');

        return [
            'user_id' => User::factory(),
            'provider' => fake()->randomElement(['strava', 'garmin', 'polar', 'suunto']),
            'external_user_id' => (string) fake()->numberBetween(100000, 999999),
            'access_token' => fake()->sha256(),
            'refresh_token' => fake()->sha256(),
            'token_expires_at' => fake()->dateTimeBetween('now', '+1 month'),
            'scopes' => fake()->randomElements(['activity:read', 'activity:read_all', 'profile:read'], 2),
            'status' => IntegrationStatus::Connected,
            'connected_at' => $connectedAt,
            'last_synced_at' => fake()->optional(0.7)->dateTimeBetween($connectedAt, 'now'),
            'meta' => null,
        ];
    }

    /** Intégration dont l'utilisateur a révoqué l'accès côté fournisseur. */
    public function revoked(): static
    {
        return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Revoked]);
    }

    /** Intégration en erreur (ex : refresh token invalide). */
    public function error(): static
    {
        return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Error]);
    }
}
