# Generated by Django 5.2.3 on 2025-06-23 02:14
import json

import django.db.models.deletion
import uuid_utils.compat
from django.db import migrations, models

from common.utils.rsa_util import rsa_long_encrypt
from lzkb.const import CONFIG
from models_provider.models import Status

default_embedding_model_id = '42f63a3d-427e-11ef-b3ec-a8a1595801ab'


def save_default_embedding_model(apps, schema_editor):
    ModelModel = apps.get_model('models_provider', 'Model')
    cache_folder = CONFIG.get('EMBEDDING_MODEL_PATH')
    model_name = CONFIG.get('EMBEDDING_MODEL_NAME')
    credential = {'cache_folder': cache_folder}
    model_credential_str = json.dumps(credential)
    model = ModelModel(id=default_embedding_model_id, name='lzkb-embedding', status=Status.SUCCESS,
                       model_type="EMBEDDING", model_name=model_name, user_id='f0dd8f71-e4ee-11ee-8c84-a8a1595801ab',
                       provider='model_local_provider',
                       credential=rsa_long_encrypt(model_credential_str), meta={},
                       workspace_id='default')
    model.save()


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('users', '0001_initial'),
        ('system_manage', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Model',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(db_index=True, max_length=128, verbose_name='名称')),
                ('status', models.CharField(choices=[('SUCCESS', '成功'), ('ERROR', '失败'), ('DOWNLOAD', '下载中'), ('PAUSE_DOWNLOAD', '暂停下载')], db_index=True, default='SUCCESS', max_length=20, verbose_name='设置类型')),
                ('model_type', models.CharField(db_index=True, max_length=128, verbose_name='模型类型')),
                ('model_name', models.CharField(db_index=True, max_length=128, verbose_name='模型名称')),
                ('provider', models.CharField(db_index=True, max_length=128, verbose_name='供应商')),
                ('credential', models.CharField(max_length=102400, verbose_name='模型认证信息')),
                ('meta', models.JSONField(default=dict, verbose_name='模型元数据,用于存储下载,或者错误信息')),
                ('model_params_form', models.JSONField(default=list, verbose_name='模型参数配置')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
                ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.user')),
            ],
            options={
                'db_table': 'model',
                'unique_together': {('name', 'workspace_id')},
            },
        ),
        migrations.RunPython(save_default_embedding_model),
    ]
