<?php

namespace Database\Factories;

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

class TaskFactory extends Factory
{
    protected $model = Task::class;

    public function definition(): array
    {
        $title = ucfirst($this->faker->unique()->words(4, true));
        $description = $this->faker->paragraph(2);

        return [
            'tenant_id' => Tenant::factory(),
            'slug' => Str::slug($title . '-' . $this->faker->unique()->numberBetween(1, 9999)),
            'title' => [
                'de' => $title,
                'en' => $title,
            ],
            'description' => [
                'de' => $description,
                'en' => $description,
            ],
            'example_text' => [
                'de' => $this->faker->sentence(),
                'en' => $this->faker->sentence(),
            ],
            'due_date' => $this->faker->dateTimeBetween('now', '+1 month'),
            'is_completed' => $this->faker->boolean(20), // 20% chance completed
            'collection_id' => null,
        ];
    }

    public function completed(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_completed' => true,
            'completed_at' => now(),
        ]);
    }

    public function urgent(): static
    {
        return $this->state(fn (array $attributes) => [
            'priority' => 'urgent',
            'due_date' => $this->faker->dateTimeBetween('now', '+3 days'),
        ]);
    }

    public function withCollection(): static
    {
        return $this->state(fn (array $attributes) => [
            'collection_id' => TaskCollection::factory(),
        ]);
    }

    public function assignedToEvent(): static
    {
        return $this->afterCreating(function (Task $task) {
            $event = \App\Models\Event::factory()->create(['tenant_id' => $task->tenant_id]);
            $task->assignedEvents()->attach($event);
        });
    }
}
