# Generated by Django 5.2.13 on 2026-05-23 11:12

import django.db.models.deletion
import llm_manager.encryption
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='LLMApi',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100, unique=True)),
                ('api_type', models.CharField(choices=[('openai', 'OpenAI Compatible'), ('azure', 'Azure OpenAI'), ('ollama', 'Ollama'), ('anthropic', 'Anthropic'), ('llama-cpp', 'Llama.cpp'), ('vllm', 'vLLM'), ('bedrock', 'Amazon Bedrock')], max_length=20)),
                ('base_url', models.URLField()),
                ('api_key', llm_manager.encryption.EncryptedCharField(blank=True, default='', max_length=500)),
                ('is_active', models.BooleanField(default=True)),
                ('supports_streaming', models.BooleanField(default=True)),
                ('timeout_seconds', models.PositiveIntegerField(default=60)),
                ('max_retries', models.PositiveIntegerField(default=3)),
                ('last_tested_at', models.DateTimeField(blank=True, help_text='Last time this API was tested', null=True)),
                ('last_test_status', models.CharField(choices=[('success', 'Success'), ('failed', 'Failed'), ('pending', 'Pending')], default='pending', help_text='Result of the last API test', max_length=20)),
                ('last_test_message', models.TextField(blank=True, help_text='Details from the last test (success message or error)')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='llm_apis_created', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'LLM API',
                'verbose_name_plural': 'LLM APIs',
                'ordering': ['name'],
            },
        ),
        migrations.CreateModel(
            name='LLMModel',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100)),
                ('display_name', models.CharField(blank=True, max_length=200)),
                ('model_type', models.CharField(choices=[('chat', 'Chat/Completion'), ('embedding', 'Embedding'), ('vision', 'Vision'), ('audio', 'Audio'), ('reranker', 'Reranker'), ('multimodal_embed', 'Multimodal Embedding')], max_length=20)),
                ('context_window', models.PositiveIntegerField(help_text='Maximum context in tokens')),
                ('max_output_tokens', models.PositiveIntegerField(blank=True, null=True)),
                ('supports_cache', models.BooleanField(default=False)),
                ('supports_vision', models.BooleanField(default=False)),
                ('supports_function_calling', models.BooleanField(default=False)),
                ('supports_json_mode', models.BooleanField(default=False)),
                ('supports_multimodal', models.BooleanField(default=False, help_text='Flag models that accept image+text input')),
                ('vector_dimensions', models.PositiveIntegerField(blank=True, help_text='Embedding output dimensions (e.g., 4096)', null=True)),
                ('input_cost_per_1k', models.DecimalField(decimal_places=6, default=Decimal('0'), help_text='Cost per 1K input tokens in USD', max_digits=10)),
                ('output_cost_per_1k', models.DecimalField(decimal_places=6, default=Decimal('0'), help_text='Cost per 1K output tokens in USD', max_digits=10)),
                ('cached_cost_per_1k', models.DecimalField(blank=True, decimal_places=6, help_text='Cost per 1K cached tokens (if supported)', max_digits=10, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('is_system_embedding_model', models.BooleanField(default=False, help_text='Mark this as the system-wide embedding model. Only ONE embedding model should have this set to True.')),
                ('is_system_chat_model', models.BooleanField(default=False, help_text='Mark this as the system-wide chat model. Only ONE chat model should have this set to True.')),
                ('is_system_reranker_model', models.BooleanField(default=False, help_text='Mark this as the system-wide reranker model. Only ONE reranker model should have this set to True.')),
                ('is_system_vision_model', models.BooleanField(default=False, help_text='Mark this as the system-wide vision model for image analysis. Only ONE vision model should have this set to True.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('api', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='models', to='llm_manager.llmapi')),
            ],
            options={
                'ordering': ['api', 'name'],
            },
        ),
        migrations.CreateModel(
            name='LLMUsage',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('timestamp', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('input_tokens', models.PositiveIntegerField(default=0)),
                ('output_tokens', models.PositiveIntegerField(default=0)),
                ('cached_tokens', models.PositiveIntegerField(default=0)),
                ('total_cost', models.DecimalField(decimal_places=6, default=Decimal('0'), help_text='Total cost in USD', max_digits=12)),
                ('session_id', models.CharField(blank=True, db_index=True, max_length=100)),
                ('purpose', models.CharField(choices=[('responder', 'RAG Responder'), ('reviewer', 'RAG Reviewer'), ('embeddings', 'Document Embeddings'), ('search', 'Vector Search'), ('reranking', 'Re-ranking'), ('multimodal_embed', 'Multimodal Embedding'), ('vision_analysis', 'Vision Analysis'), ('other', 'Other')], db_index=True, default='other', max_length=50)),
                ('request_metadata', models.JSONField(blank=True, help_text='Additional context (prompt, temperature, etc.)', null=True)),
                ('model', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='usage_records', to='llm_manager.llmmodel')),
                ('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='llm_usage', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-timestamp'],
            },
        ),
        migrations.AddIndex(
            model_name='llmmodel',
            index=models.Index(fields=['api', 'model_type', 'is_active'], name='llm_manager_api_id_140af0_idx'),
        ),
        migrations.AddIndex(
            model_name='llmmodel',
            index=models.Index(fields=['is_system_embedding_model', 'model_type'], name='llm_manager_is_syst_39386f_idx'),
        ),
        migrations.AddIndex(
            model_name='llmmodel',
            index=models.Index(fields=['is_system_chat_model', 'model_type'], name='llm_manager_is_syst_346eb3_idx'),
        ),
        migrations.AddIndex(
            model_name='llmmodel',
            index=models.Index(fields=['is_system_reranker_model', 'model_type'], name='llm_manager_is_syst_cc73c6_idx'),
        ),
        migrations.AddIndex(
            model_name='llmmodel',
            index=models.Index(fields=['is_system_vision_model', 'model_type'], name='llm_manager_is_syst_d190bb_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='llmmodel',
            unique_together={('api', 'name')},
        ),
        migrations.AddIndex(
            model_name='llmusage',
            index=models.Index(fields=['-timestamp', 'user'], name='llm_manager_timesta_aa66fc_idx'),
        ),
        migrations.AddIndex(
            model_name='llmusage',
            index=models.Index(fields=['-timestamp', 'model'], name='llm_manager_timesta_0b5c38_idx'),
        ),
        migrations.AddIndex(
            model_name='llmusage',
            index=models.Index(fields=['purpose', '-timestamp'], name='llm_manager_purpose_37c32c_idx'),
        ),
        migrations.AddIndex(
            model_name='llmusage',
            index=models.Index(fields=['session_id'], name='llm_manager_session_1da37d_idx'),
        ),
    ]
