# Generated by Django 5.2.10 on 2026-03-02 15:41

from decimal import Decimal

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


class Migration(migrations.Migration):

    dependencies = [
        ("decision_rhythm", "0003_add_valuation_and_approval"),
    ]

    operations = [
        migrations.AddField(
            model_name="executionapprovalrequestmodel",
            name="execution_params_json",
            field=models.JSONField(
                blank=True, default=dict, help_text="执行参数 JSON（价格区间、仓位等）", null=True
            ),
        ),
        migrations.CreateModel(
            name="DecisionFeatureSnapshotModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "snapshot_id",
                    models.CharField(
                        db_index=True, help_text="快照唯一标识符", max_length=64, unique=True
                    ),
                ),
                (
                    "security_code",
                    models.CharField(db_index=True, help_text="证券代码", max_length=32),
                ),
                ("snapshot_time", models.DateTimeField(db_index=True, help_text="快照时间")),
                (
                    "regime",
                    models.CharField(default="", help_text="当前 Regime 状态", max_length=64),
                ),
                ("regime_confidence", models.FloatField(default=0.0, help_text="Regime 置信度")),
                ("policy_level", models.CharField(default="", help_text="政策档位", max_length=32)),
                (
                    "beta_gate_passed",
                    models.BooleanField(default=False, help_text="Beta Gate 是否通过"),
                ),
                ("sentiment_score", models.FloatField(default=0.0, help_text="舆情分数")),
                ("flow_score", models.FloatField(default=0.0, help_text="资金流向分数")),
                ("technical_score", models.FloatField(default=0.0, help_text="技术面分数")),
                ("fundamental_score", models.FloatField(default=0.0, help_text="基本面分数")),
                ("alpha_model_score", models.FloatField(default=0.0, help_text="Alpha 模型分数")),
                ("extra_features", models.JSONField(default=dict, help_text="额外特征")),
                ("created_at", models.DateTimeField(auto_now_add=True, help_text="创建时间")),
            ],
            options={
                "verbose_name": "决策特征快照",
                "verbose_name_plural": "决策特征快照",
                "db_table": "decision_feature_snapshot",
                "ordering": ["-snapshot_time"],
                "indexes": [
                    models.Index(
                        fields=["security_code", "-snapshot_time"], name="idx_fsn_sec_time"
                    )
                ],
            },
        ),
        migrations.AddField(
            model_name="decisionrequestmodel",
            name="feature_snapshot",
            field=models.ForeignKey(
                blank=True,
                help_text="关联的特征快照",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="decision_requests",
                to="decision_rhythm.decisionfeaturesnapshotmodel",
            ),
        ),
        migrations.CreateModel(
            name="DecisionModelParamAuditLogModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "log_id",
                    models.CharField(
                        db_index=True, help_text="日志唯一标识符", max_length=64, unique=True
                    ),
                ),
                ("param_key", models.CharField(db_index=True, help_text="参数键", max_length=128)),
                ("old_value", models.TextField(help_text="旧值")),
                ("new_value", models.TextField(help_text="新值")),
                (
                    "env",
                    models.CharField(
                        choices=[("dev", "开发环境"), ("test", "测试环境"), ("prod", "生产环境")],
                        db_index=True,
                        default="dev",
                        help_text="环境",
                        max_length=16,
                    ),
                ),
                ("changed_by", models.CharField(default="", help_text="变更人", max_length=128)),
                ("change_reason", models.TextField(blank=True, help_text="变更原因")),
                ("changed_at", models.DateTimeField(db_index=True, help_text="变更时间")),
            ],
            options={
                "verbose_name": "决策模型参数审计日志",
                "verbose_name_plural": "决策模型参数审计日志",
                "db_table": "decision_model_param_audit_log",
                "ordering": ["-changed_at"],
                "indexes": [
                    models.Index(fields=["param_key", "-changed_at"], name="idx_audit_key_time"),
                    models.Index(fields=["env", "-changed_at"], name="idx_audit_env_time"),
                ],
            },
        ),
        migrations.CreateModel(
            name="DecisionModelParamConfigModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "config_id",
                    models.CharField(
                        db_index=True, help_text="配置唯一标识符", max_length=64, unique=True
                    ),
                ),
                ("param_key", models.CharField(db_index=True, help_text="参数键", max_length=128)),
                ("param_value", models.TextField(help_text="参数值")),
                (
                    "param_type",
                    models.CharField(
                        choices=[
                            ("float", "浮点数"),
                            ("int", "整数"),
                            ("str", "字符串"),
                            ("bool", "布尔值"),
                        ],
                        default="float",
                        help_text="参数类型",
                        max_length=16,
                    ),
                ),
                (
                    "env",
                    models.CharField(
                        choices=[("dev", "开发环境"), ("test", "测试环境"), ("prod", "生产环境")],
                        db_index=True,
                        default="dev",
                        help_text="环境",
                        max_length=16,
                    ),
                ),
                ("version", models.IntegerField(default=1, help_text="版本号")),
                (
                    "is_active",
                    models.BooleanField(db_index=True, default=True, help_text="是否激活"),
                ),
                ("description", models.TextField(blank=True, help_text="参数描述")),
                (
                    "updated_by",
                    models.CharField(default="", help_text="最后修改人", max_length=128),
                ),
                ("updated_reason", models.TextField(blank=True, help_text="变更说明")),
                ("created_at", models.DateTimeField(auto_now_add=True, help_text="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, help_text="更新时间")),
            ],
            options={
                "verbose_name": "决策模型参数配置",
                "verbose_name_plural": "决策模型参数配置",
                "db_table": "decision_model_param_config",
                "ordering": ["env", "param_key"],
                "indexes": [
                    models.Index(
                        fields=["env", "param_key", "is_active"], name="idx_param_env_key_active"
                    ),
                    models.Index(
                        fields=["param_key", "env", "-version"], name="idx_param_key_env_version"
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="UnifiedRecommendationModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "recommendation_id",
                    models.CharField(
                        db_index=True, help_text="推荐唯一标识符", max_length=64, unique=True
                    ),
                ),
                ("account_id", models.CharField(db_index=True, help_text="账户 ID", max_length=64)),
                (
                    "security_code",
                    models.CharField(db_index=True, help_text="证券代码", max_length=32),
                ),
                (
                    "side",
                    models.CharField(
                        choices=[("BUY", "买入"), ("SELL", "卖出"), ("HOLD", "持有")],
                        help_text="方向",
                        max_length=8,
                    ),
                ),
                (
                    "regime",
                    models.CharField(default="", help_text="当前 Regime 状态", max_length=64),
                ),
                ("regime_confidence", models.FloatField(default=0.0, help_text="Regime 置信度")),
                ("policy_level", models.CharField(default="", help_text="政策档位", max_length=32)),
                (
                    "beta_gate_passed",
                    models.BooleanField(
                        db_index=True, default=False, help_text="Beta Gate 是否通过"
                    ),
                ),
                ("sentiment_score", models.FloatField(default=0.0, help_text="舆情分数")),
                ("flow_score", models.FloatField(default=0.0, help_text="资金流向分数")),
                ("technical_score", models.FloatField(default=0.0, help_text="技术面分数")),
                ("fundamental_score", models.FloatField(default=0.0, help_text="基本面分数")),
                ("alpha_model_score", models.FloatField(default=0.0, help_text="Alpha 模型分数")),
                (
                    "composite_score",
                    models.FloatField(db_index=True, default=0.0, help_text="综合分数"),
                ),
                ("confidence", models.FloatField(default=0.0, help_text="置信度")),
                ("reason_codes", models.JSONField(default=list, help_text="原因代码列表")),
                ("human_rationale", models.TextField(blank=True, help_text="人类可读理由")),
                (
                    "fair_value",
                    models.DecimalField(
                        decimal_places=4, default=Decimal("0"), help_text="公允价值", max_digits=12
                    ),
                ),
                (
                    "entry_price_low",
                    models.DecimalField(
                        decimal_places=4,
                        default=Decimal("0"),
                        help_text="入场价格下限",
                        max_digits=12,
                    ),
                ),
                (
                    "entry_price_high",
                    models.DecimalField(
                        decimal_places=4,
                        default=Decimal("0"),
                        help_text="入场价格上限",
                        max_digits=12,
                    ),
                ),
                (
                    "target_price_low",
                    models.DecimalField(
                        decimal_places=4,
                        default=Decimal("0"),
                        help_text="目标价格下限",
                        max_digits=12,
                    ),
                ),
                (
                    "target_price_high",
                    models.DecimalField(
                        decimal_places=4,
                        default=Decimal("0"),
                        help_text="目标价格上限",
                        max_digits=12,
                    ),
                ),
                (
                    "stop_loss_price",
                    models.DecimalField(
                        decimal_places=4, default=Decimal("0"), help_text="止损价格", max_digits=12
                    ),
                ),
                ("position_pct", models.FloatField(default=5.0, help_text="建议仓位比例")),
                ("suggested_quantity", models.IntegerField(default=0, help_text="建议数量")),
                (
                    "max_capital",
                    models.DecimalField(
                        decimal_places=2,
                        default=Decimal("50000"),
                        help_text="最大资金量",
                        max_digits=15,
                    ),
                ),
                ("source_signal_ids", models.JSONField(default=list, help_text="来源信号 ID 列表")),
                (
                    "source_candidate_ids",
                    models.JSONField(default=list, help_text="来源候选 ID 列表"),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("NEW", "新建"),
                            ("REVIEWING", "审核中"),
                            ("APPROVED", "已批准"),
                            ("REJECTED", "已拒绝"),
                            ("EXECUTED", "已执行"),
                            ("FAILED", "执行失败"),
                            ("CONFLICT", "冲突"),
                        ],
                        db_index=True,
                        default="NEW",
                        help_text="推荐状态",
                        max_length=16,
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, db_index=True, help_text="创建时间"),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, help_text="更新时间")),
                (
                    "feature_snapshot",
                    models.ForeignKey(
                        blank=True,
                        help_text="关联的特征快照",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="unified_recommendations",
                        to="decision_rhythm.decisionfeaturesnapshotmodel",
                    ),
                ),
            ],
            options={
                "verbose_name": "统一推荐",
                "verbose_name_plural": "统一推荐",
                "db_table": "decision_unified_recommendation",
                "ordering": ["-composite_score", "-created_at"],
            },
        ),
        migrations.AddField(
            model_name="decisionrequestmodel",
            name="unified_recommendation",
            field=models.ForeignKey(
                blank=True,
                help_text="关联的统一推荐",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="decision_requests",
                to="decision_rhythm.unifiedrecommendationmodel",
            ),
        ),
        migrations.AddField(
            model_name="executionapprovalrequestmodel",
            name="unified_recommendation",
            field=models.ForeignKey(
                blank=True,
                help_text="关联的统一推荐",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="approval_requests_v2",
                to="decision_rhythm.unifiedrecommendationmodel",
            ),
        ),
        migrations.AddIndex(
            model_name="decisionrequestmodel",
            index=models.Index(
                fields=["unified_recommendation"], name="decision_re_unified_168032_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="unifiedrecommendationmodel",
            index=models.Index(
                fields=["account_id", "security_code", "side", "-created_at"],
                name="idx_urec_acc_sec_side",
            ),
        ),
        migrations.AddIndex(
            model_name="unifiedrecommendationmodel",
            index=models.Index(fields=["status", "-composite_score"], name="idx_urec_status_score"),
        ),
        migrations.AddIndex(
            model_name="unifiedrecommendationmodel",
            index=models.Index(fields=["beta_gate_passed", "status"], name="idx_urec_gate_status"),
        ),
    ]
