<?php

namespace Database\Factories;

use App\Enums\AccountStatusEnum;
use App\Enums\PlatformEnum;
use App\Models\PlatformAccount;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<PlatformAccount>
 */
class PlatformAccountFactory extends Factory
{
    protected $model = PlatformAccount::class;

    public function definition(): array
    {
        return [
            'platform' => PlatformEnum::LEMMY,
            'instance_url' => 'https://lemmy.'.$this->faker->domainName(),
            'username' => $this->faker->unique()->userName(),
            'password' => 'test-password',
            'settings' => [],
            'is_active' => true,
            'last_tested_at' => null,
            'status' => AccountStatusEnum::UNTESTED,
        ];
    }

    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
        ]);
    }

    public function tested(): static
    {
        return $this->state(fn (array $attributes) => [
            'last_tested_at' => now()->subHours(2),
            'status' => AccountStatusEnum::HEALTHY,
        ]);
    }

    public function failed(): static
    {
        return $this->state(fn (array $attributes) => [
            'last_tested_at' => now()->subHours(2),
            'status' => AccountStatusEnum::UNHEALTHY,
            'consecutive_failures' => PlatformAccount::FAILURES_BEFORE_UNHEALTHY,
        ]);
    }
}
