<?php
declare(strict_types=1);

use Elastic\Adapter\Indices\Mapping;
use Elastic\Adapter\Indices\Settings;
use Elastic\Migrations\Facades\Index;
use Elastic\Migrations\MigrationInterface;
use Elastic\Elasticsearch\ClientBuilder;

final class CreateClientContactsIndex implements MigrationInterface
{
    /**
     * Run the migration.
     */
    public function up(): void
    {
        // Check if index already exists (idempotency)
        $client = ClientBuilder::fromConfig(config('elastic.client.connections.default'));
       
        $indexExistsResponse = $client->indices()->exists(['index' => 'client_contacts']);
        if ($indexExistsResponse->getStatusCode() === 200) {
            return;
        }

        $mapping = [
            'properties' => [
                // Core client contact fields
                'id' => ['type' => 'keyword'],
                'name' => [
                    'type' => 'text',
                    'analyzer' => 'standard'
                ],
                'is_deleted' => ['type' => 'boolean'],
                'hashed_id' => ['type' => 'keyword'],
                'first_name' => ['type' => 'keyword'],
                'last_name' => ['type' => 'keyword'],
                'email' => ['type' => 'keyword'],
                'phone' => ['type' => 'keyword'],
                'is_primary' => ['type' => 'boolean'],
                
                // Custom fields
                'custom_value1' => ['type' => 'keyword'],
                'custom_value2' => ['type' => 'keyword'],
                'custom_value3' => ['type' => 'keyword'],
                'custom_value4' => ['type' => 'keyword'],
                
                // Additional fields
                'company_key' => ['type' => 'keyword'],
                'client_id' => ['type' => 'keyword'],
                'send_email' => ['type' => 'boolean'],
                'last_login' => ['type' => 'date'],
            ]
        ];

        Index::createRaw('client_contacts', $mapping);
    }

    /**
     * Reverse the migration.
     */
    public function down(): void
    {
        Index::dropIfExists('client_contacts');
    }
}
