<?php

namespace Database\Factories;

use App\Models\PlatformConnection;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<PlatformConnection>
 */
class PlatformConnectionFactory extends Factory
{
    protected $model = PlatformConnection::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'user_id' => null,
            'platform' => 'meta',
            'name' => 'Default Platform Connection',
            'account_id' => 'act_'.$this->faker->numerify('################'),
            'access_token' => 'mock_access_token_'.$this->faker->sha256(),
            'refresh_token' => 'mock_refresh_token_'.$this->faker->sha256(),
            'token_type' => 'Bearer',
            'expires_at' => now()->addDays(60),
            'refresh_expires_at' => now()->addDays(365),
            'last_refreshed_at' => now(),
            'scopes' => ['ads_management', 'ads_read'],
            'platform_data' => null,
            'is_active' => true,
            'last_error' => null,
        ];
    }

    /** Meta platform state */
    public function meta(?string $token = null): static
    {
        return $this->state(fn (array $attributes): array => [
            'platform' => 'meta',
            'name' => 'Meta Connection',
            'access_token' => $token ?? 'EAAB'.$this->faker->sha256(),
            'refresh_token' => null, // Meta long-lived tokens are refreshed via exchange token, not standard refresh_token
            'expires_at' => now()->addDays(60),
            'scopes' => ['ads_management', 'ads_read', 'business_management'],
        ]);
    }

    /** Google Ads platform state */
    public function google(?string $customerId = null, ?string $refreshToken = null): static
    {
        return $this->state(fn (array $attributes): array => [
            'platform' => 'google',
            'name' => 'Google Ads Connection',
            'account_id' => $customerId ?? $this->faker->numerify('##########'),
            'access_token' => 'ya29.'.$this->faker->sha256(),
            'refresh_token' => $refreshToken ?? '1//'.$this->faker->sha256(),
            'expires_at' => now()->addHours(1),
            'scopes' => ['https://www.googleapis.com/auth/adwords'],
            'platform_data' => [
                'manager_customer_id' => $this->faker->numerify('##########'),
                'developer_token' => 'mock_dev_token_'.$this->faker->lexify('????????'),
            ],
        ]);
    }

    /** LinkedIn platform state */
    public function linkedin(?string $orgId = null): static
    {
        return $this->state(fn (array $attributes): array => [
            'platform' => 'linkedin',
            'name' => 'LinkedIn Connection',
            'account_id' => $orgId ?? 'urn:li:organization:'.$this->faker->numerify('#######'),
            'access_token' => 'AQV'.$this->faker->sha256(),
            'refresh_token' => 'AQU'.$this->faker->sha256(),
            'expires_at' => now()->addDays(60),
            'refresh_expires_at' => now()->addDays(365),
            'scopes' => ['r_ads', 'rw_ads', 'w_member_social'],
        ]);
    }

    /** For a specific user */
    public function forUser(User $user): static
    {
        return $this->state(fn (array $attributes): array => [
            'user_id' => $user->id,
        ]);
    }

    /** Expiring soon (within 7 days) */
    public function expiringSoon(int $days = 3): static
    {
        return $this->state(fn (array $attributes): array => [
            'expires_at' => now()->addDays($days),
        ]);
    }

    /** Already expired */
    public function expired(): static
    {
        return $this->state(fn (array $attributes): array => [
            'expires_at' => now()->subDay(),
        ]);
    }

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