<?php

namespace Database\Factories;

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

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BackupSet>
 */
class BackupSetFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'managed_environment_id' => ManagedEnvironment::factory(),
            'name' => fake()->words(3, true),
            'created_by' => fake()->email(),
            'status' => 'completed',
            'item_count' => 0,
            'completed_at' => now(),
            'metadata' => [],
        ];
    }

    public function minimal(): static
    {
        return $this->state(fn (): array => [
            'item_count' => 0,
            'metadata' => [],
        ]);
    }

    /**
     * @param  array<string, mixed>  $itemAttributes
     */
    public function withItems(int $count = 1, array $itemAttributes = []): static
    {
        $count = max(1, $count);

        return $this->state(fn (): array => [
            'item_count' => $count,
        ])->afterCreating(function ($backupSet) use ($count, $itemAttributes): void {
            BackupItem::factory()
                ->count($count)
                ->for($backupSet->tenant)
                ->for($backupSet)
                ->create(array_merge([
                    'payload' => ['id' => 'backup-item-'.fake()->uuid()],
                    'metadata' => [],
                    'assignments' => [],
                ], $itemAttributes));
        });
    }

    public function full(): static
    {
        return $this->recentCompleted()->withItems();
    }

    public function recentCompleted(): static
    {
        return $this->state(fn (): array => [
            'status' => 'completed',
            'completed_at' => now()->subMinutes(20),
        ]);
    }

    public function staleCompleted(): static
    {
        return $this->state(fn (): array => [
            'status' => 'completed',
            'completed_at' => now()->subDays(2),
            'item_count' => 1,
        ])->afterCreating(function ($backupSet): void {
            BackupItem::factory()
                ->for($backupSet->tenant)
                ->for($backupSet)
                ->create([
                    'payload' => ['id' => 'stale-backup-item'],
                    'metadata' => [],
                    'assignments' => [],
                ]);
        });
    }

    public function degradedCompleted(): static
    {
        return $this->state(fn (): array => [
            'status' => 'completed',
            'completed_at' => now()->subMinutes(30),
            'item_count' => 1,
        ])->afterCreating(function ($backupSet): void {
            BackupItem::factory()
                ->for($backupSet->tenant)
                ->for($backupSet)
                ->create([
                    'payload' => [],
                    'metadata' => [
                        'source' => 'metadata_only',
                        'assignments_fetch_failed' => true,
                    ],
                    'assignments' => [],
                ]);
        });
    }
}
