from typesense.exceptions import TypesenseClientError, ObjectAlreadyExists

from utils.typesense_client import client

# Create a collection
try:
    client.collections.create({
        'name': 'social_media_posts',
        'fields': [
            {'name': 'id', 'type': 'string'},
            {'name': 'network', 'type': 'string', 'facet': True},
            {'name': 'type', 'type': 'string', 'facet': True},
            {'name': 'index', 'type': 'string', 'facet': True},
            {'name': 'chemin', 'type': 'string'},
            {'name': 'texte', 'type': 'string'},
            {'name': 'creation_timestamp', 'type': 'int64'},
            {
                "name" : "embedding",
                "type" : "float[]",
                "embed": {
                    "from": [
                        "texte"
                    ],
                    "model_config": {
                        "model_name": "ts/multilingual-e5-small"
                    }
                }
            }
        ],
        'default_sorting_field': 'creation_timestamp'
    })
    print("Collection 'social_media_posts' created successfully.")
except TypesenseClientError as e:
    if e==ObjectAlreadyExists:
        print("Collection 'social_media_posts' already exists. Skipping creation.")
    else:
        print(f"Error creating collection: {str(e)}")
        raise


