<?php

namespace Database\Factories\Clients;

use App\Models\Clients\Client;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Clients\Client>
 */
class ClientFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Model>
     */
    protected $model = Client::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $name = fake()->company();
        $shortname = strtolower(str_replace([' ', '-', '.', ','], '-', $name));
        $shortname = substr($shortname, 0, 50);

        $contacts = null;
        if (fake()->boolean(70)) {
            $contacts = json_encode([
                'name' => fake()->name(),
                'email' => fake()->companyEmail(),
                'phone' => fake()->phoneNumber(),
            ]);
        }

        $crm_id = null;
        if (fake()->boolean(70)) {
            $crm_id = fake()->unique()->numerify('CRM-####');
        }

        return [
            'name' => $name,
            'shortname' => $shortname . '-' . fake()->unique()->numberBetween(1000, 9999),
            'crm_id' => $crm_id,
            'cif' => fake()->optional(0.6)->regexify('[A-Z][0-9]{8}'),
            'razon_social' => fake()->optional(0.6)->company(),
            'fecha_inicio_relacion' => fake()->optional(0.7)->dateTimeBetween('-5 years', 'now'),
            'description' => fake()->optional(0.8)->paragraph(3),
            'contacts' => $contacts,
            'jira' => fake()->optional(0.5)->regexify('[A-Z]{2,4}-[0-9]{1,4}'),
            'actived' => fake()->boolean(90), // 90% de probabilidad de estar activo
            'metadata' => fake()->optional(0.4)->randomElement([
                ['country' => 'ES', 'industry' => 'Technology'],
                ['country' => 'ES', 'industry' => 'Retail'],
                ['country' => 'ES', 'industry' => 'Healthcare'],
                ['country' => 'ES', 'industry' => 'Finance'],
                ['country' => 'ES', 'industry' => 'Education'],
            ]),
            'observation' => fake()->optional(0.3)->sentence(10),
        ];
    }

    /**
     * Indica que el cliente está activo.
     */
    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'actived' => true,
        ]);
    }

    /**
     * Indica que el cliente está inactivo.
     */
    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'actived' => false,
        ]);
    }

    /**
     * Indica que el cliente tiene información completa.
     */
    public function complete(): static
    {
        return $this->state(fn (array $attributes) => [
            'crm_id' => fake()->unique()->numerify('CRM-####'),
            'cif' => fake()->regexify('[A-Z][0-9]{8}'),
            'razon_social' => fake()->company(),
            'fecha_inicio_relacion' => fake()->dateTimeBetween('-5 years', 'now'),
            'description' => fake()->paragraph(3),
            'contacts' => json_encode([
                'name' => fake()->name(),
                'email' => fake()->companyEmail(),
                'phone' => fake()->phoneNumber(),
            ]),
            'jira' => fake()->regexify('[A-Z]{2,4}-[0-9]{1,4}'),
            'metadata' => [
                'country' => fake()->randomElement(['ES', 'FR', 'IT', 'DE', 'PT']),
                'industry' => fake()->randomElement(['Technology', 'Retail', 'Healthcare', 'Finance', 'Education', 'Manufacturing']),
            ],
            'observation' => fake()->sentence(10),
        ]);
    }
}

