<?php

namespace Database\Factories\CRM;

use App\Models\CRM\City;
use App\Enums\AddressType;
use App\Models\CRM\Address;
use App\Models\CRM\Individual;
use Illuminate\Database\Eloquent\Factories\Factory;

class AddressFactory extends Factory
{
    protected $model = Address::class;

    /**
     * Automatically associate an Individual if none was provided.
     */
    public function configure(): static
    {
        return $this->afterMaking(function (Address $address) {
            if (is_null($address->addressable_id)) {
                // create a fresh Individual and associate
                $individual = Individual::factory()->create();
                $address->addressable()->associate($individual);
            }
        });
    }

    public function definition(): array
    {
        return [
            'addressable_id'   => null,    // will be populated by afterMaking
            'addressable_type' => null,
            'line_1'           => $this->faker->streetAddress(),
            'type'             => AddressType::Residence,
            'city_id'          => City::factory(),
            'status'           => 'active',
        ];
    }
}
