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

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

from common.utils.common import get_file_content
from lzkb.const import PROJECT_DIR
from tools.models import ToolFolder


def insert_default_data(apps, schema_editor):
    # 创建一个根模块（没有父节点）
    ToolFolder.objects.create(id='default', name='根目录', user_id='f0dd8f71-e4ee-11ee-8c84-a8a1595801ab', workspace_id='default')


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name='ToolFolder',
            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.CharField(editable=False, max_length=64, primary_key=True, serialize=False, verbose_name='主键id')),
                ('name', models.CharField(db_index=True, max_length=64, verbose_name='文件夹名称')),
                ('desc', models.CharField(blank=True, max_length=200, null=True, verbose_name='描述')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
                ('lft', models.PositiveIntegerField(editable=False)),
                ('rght', models.PositiveIntegerField(editable=False)),
                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
                ('level', models.PositiveIntegerField(editable=False)),
                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='tools.toolfolder')),
                ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.user')),
            ],
            options={
                'db_table': 'tool_folder',
            },
        ),
        migrations.RunPython(insert_default_data),
        migrations.CreateModel(
            name='Tool',
            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=64, verbose_name='工具名称')),
                ('desc', models.CharField(max_length=128, verbose_name='描述')),
                ('code', models.CharField(max_length=102400, verbose_name='python代码')),
                ('input_field_list', models.JSONField(default=list, verbose_name='输入字段列表')),
                ('init_field_list', models.JSONField(default=list, verbose_name='启动字段列表')),
                ('icon', models.CharField(default='', max_length=256, verbose_name='工具库icon')),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('scope', models.CharField(choices=[('SHARED', '共享'), ('WORKSPACE', '工作空间可用'), ('INTERNAL', '内置')], db_index=True, default='WORKSPACE', max_length=20, verbose_name='可用范围')),
                ('tool_type', models.CharField(choices=[('INTERNAL', '内置'), ('CUSTOM', '自定义')], db_index=True, default='CUSTOM', max_length=20, verbose_name='工具类型')),
                ('template_id', models.UUIDField(db_index=True, default=None, null=True, verbose_name='模版id')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
                ('init_params', models.CharField(max_length=102400, null=True, verbose_name='初始化参数')),
                ('label', models.CharField(db_index=True, max_length=128, null=True, verbose_name='标签')),
                ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.user')),
                ('folder', models.ForeignKey(default='default', on_delete=django.db.models.deletion.DO_NOTHING, to='tools.toolfolder', verbose_name='文件夹id')),
            ],
            options={
                'db_table': 'tool',
            },
        ),
        migrations.RunSQL(get_file_content(os.path.join(PROJECT_DIR, "apps", "tools", 'migrations', 'internal_tool.sql')))
    ]
