<?php

namespace Database\Factories;

use App\Models\Emotion;
use App\Models\Event;
use App\Models\Photo;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class PhotoFactory extends Factory
{
    protected $model = Photo::class;

    public function definition(): array
    {
        return [
            'event_id' => Event::factory(),
            'emotion_id' => Emotion::factory(),
            'task_id' => null,
            'guest_name' => $this->faker->name(),
            'file_path' => 'photos/'.Str::uuid().'.jpg',
            'thumbnail_path' => 'photos/thumbnails/'.Str::uuid().'.jpg',
            'likes_count' => $this->faker->numberBetween(0, 25),
            'is_featured' => false,
            'metadata' => ['factory' => true],
            'ingest_source' => \App\Models\Photo::SOURCE_GUEST_PWA,
        ];
    }

    public function configure(): static
    {
        return $this->afterMaking(function (Photo $photo) {
            if (! $photo->tenant_id && $photo->event) {
                $photo->tenant_id = $photo->event->tenant_id;
            }
        })->afterCreating(function (Photo $photo) {
            if ($photo->event && ! $photo->tenant_id) {
                $photo->tenant_id = $photo->event->tenant_id;
                $photo->save();
            }

            if ($photo->tenant_id && $photo->event && $photo->event->tenant_id !== $photo->tenant_id) {
                $photo->event->update(['tenant_id' => $photo->tenant_id]);
            }
        });
    }
}
