<?php

namespace Database\Factories;

use App\Models\Album;
use App\Models\Image;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Image>
 */
class ImageFactory extends Factory
{
/**
     * Configure the model factory.
     */
    public function configure(): static
    {
        return $this->afterCreating(function (Image $image) {
            if($image->album->images->sortBy('id')->first()->id == $image->id) {
                $image->isCover = true;
                $image->save();
            }

            $height = rand(2000, 4000);
            $width = rand(2000, 4000);
            $image_content = Http::get("https://picsum.photos/{$width}/{$height}")->body();
            Storage::disk('images')->put($image->album->id . '/original/' . $image->id, $image_content);
        });
    }

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'album_id' => Album::all()->random(1)->first()->id,
            'isCover' => false,
        ];
    }
}
