<?php

namespace Database\Factories;

use App\Models\Location;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Location>
 */
class LocationFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * A country, and the only type that has an iso_code: locations.iso_code is
     * a globally unique two-character country identity, and every read of it in
     * the app is scoped to type = country.
     *
     * So a row of any other type must pass 'iso_code' => null. Overriding only
     * the type leaves a country's code on a city - a city named "France"
     * holding FR - which then collides with the seeded countries through that
     * unique index, at random, because the code here is picked at random.
     * faker's unique() keeps the picks distinct within one run and knows
     * nothing about rows a seeder inserted directly.
     *
     * Two tests were doing exactly that and failing on whichever run the faker
     * landed on a seeded country.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'type' => 'country',
            'name' => config('countries.'.$code = $this->faker->unique()->randomElement(array_keys(config('countries')))),
            'country_code' => $code,
            'iso_code' => $code,
            'source' => 'iso_3166_1',
            'source_key' => $code,
        ];
    }
}
