<?php

namespace Database\Factories;

use App\Models\EventType;
use App\Models\Task;
use App\Models\TaskCollection;
use App\Models\Tenant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class TaskCollectionFactory extends Factory
{
    protected $model = TaskCollection::class;

    public function definition(): array
    {
        $label = ucfirst($this->faker->unique()->words(2, true));
        $description = $this->faker->sentence(12);

        return [
            'tenant_id' => Tenant::factory(),
            'event_type_id' => EventType::factory(),
            'slug' => Str::slug($label . '-' . $this->faker->unique()->numberBetween(1, 9999)),
            'name_translations' => [
                'de' => $label,
                'en' => $label,
            ],
            'description_translations' => [
                'de' => $description,
                'en' => $description,
            ],
            'is_default' => $this->faker->boolean(20),
            'position' => $this->faker->numberBetween(1, 10),
        ];
    }

    public function withTasks(int $count = 3): static
    {
        return $this->afterCreating(function (TaskCollection $collection) use ($count) {
            \App\Models\Task::factory($count)
                ->create([
                    'tenant_id' => $collection->tenant_id,
                    'event_type_id' => $collection->event_type_id,
                ])
                ->each(function ($task) use ($collection) {
                    $task->taskCollection()->associate($collection);
                    $task->save();
                });
        });
    }

    public function default(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_default' => true,
            'position' => 1,
        ]);
    }
}
