# Generated manually for stateflow parameter refactoring
from django.db import migrations, models


def cleanup_old_parameters(apps, schema_editor):
    """
    清空旧的参数数据（测试数据，无需保留）
    """
    StateParameter = apps.get_model('stateflow', 'StateParameter')
    # 删除所有旧的参数
    StateParameter.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('stateflow', '0016_alter_stateparameter_key_alter_stateparameter_value'),
    ]

    operations = [
        # 步骤1: 清空旧参数数据
        migrations.RunPython(
            cleanup_old_parameters,
            migrations.RunPython.noop,
        ),
        
        # 步骤2: 移除旧的 ForeignKey
        migrations.RemoveField(
            model_name='stateparameter',
            name='state',
        ),
        
        # 步骤3: 添加 name 字段
        migrations.AddField(
            model_name='stateparameter',
            name='name',
            field=models.CharField(
                default='未命名参数',
                help_text='参数的显示名称',
                max_length=100,
                verbose_name='参数名称'
            ),
            preserve_default=False,
        ),
        
        # 步骤4: 添加多对多字段
        migrations.AddField(
            model_name='state',
            name='parameters',
            field=models.ManyToManyField(
                blank=True,
                help_text='此状态使用的工艺参数',
                related_name='states',
                to='stateflow.stateparameter',
                verbose_name='关联参数'
            ),
        ),
        
        # 步骤5: 将 key 设置为 unique
        migrations.AlterField(
            model_name='stateparameter',
            name='key',
            field=models.CharField(
                help_text='唯一标识符',
                max_length=100,
                unique=True,
                verbose_name='参数键'
            ),
        ),
    ]
