<?php

namespace Database\Factories;

use App\Enums\ClientStatus;
use App\Enums\LegalForm;
use App\Models\ClientContact;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Client>
 */
class ClientFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'workspace_id' => Workspace::factory(),
            'company_name' => fake()->company(),
            'legal_form' => fake()->randomElement(LegalForm::getValues()),
            'ice' => (string) fake()->numerify('##########'),
            'fiscal_id' => (string) fake()->numerify('##########'),
            'rc' => (string) fake()->numerify('######'),
            'cnss' => (string) fake()->numerify('##########'),
            'patente' => (string) fake()->numerify('########'),
            'contact_last_name' => fake()->lastName(),
            'contact_first_name' => fake()->firstName(),
            'contact_job_title' => fake()->jobTitle(),
            'contact_email' => fake()->unique()->safeEmail(),
            'contact_phone' => fake()->phoneNumber(),
            'internal_responsible_id' => null,
            'status' => ClientStatus::Active,
            'internal_notes' => fake()->optional(0.3)->sentence(),
        ];
    }

    /**
     * Configure the model factory.
     */
    public function configure(): static
    {
        return $this->afterCreating(function ($client) {
            ClientContact::factory()->principal()->create([
                'client_id' => $client->id,
            ]);
        });
    }

    /**
     * Associate the client with an internal responsible user.
     */
    public function withInternalResponsible(User $user): static
    {
        return $this->state(fn () => ['internal_responsible_id' => $user->id]);
    }
}
