# Generated by Django 5.2.9 on 2026-01-02 13:57

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


class Migration(migrations.Migration):

    dependencies = [
        ("audit", "0001_initial"),
        ("backtest", "0004_backtestresultmodel_signal_configs_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="AttributionReport",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("period_start", models.DateField(verbose_name="分析起始日期")),
                ("period_end", models.DateField(verbose_name="分析结束日期")),
                (
                    "regime_timing_pnl",
                    models.FloatField(
                        help_text="因 Regime 判断正确/错误产生的收益/损失",
                        verbose_name="Regime 择时贡献",
                    ),
                ),
                (
                    "asset_selection_pnl",
                    models.FloatField(
                        help_text="因资产选择正确/错误产生的收益/损失", verbose_name="资产选择贡献"
                    ),
                ),
                (
                    "interaction_pnl",
                    models.FloatField(help_text="择时与选股的交互作用", verbose_name="交互效应"),
                ),
                ("total_pnl", models.FloatField(verbose_name="总收益")),
                (
                    "regime_accuracy",
                    models.FloatField(
                        help_text="实际 Regime 与预测 Regime 的匹配度（0-1）",
                        verbose_name="Regime 预测准确率",
                    ),
                ),
                ("regime_predicted", models.CharField(max_length=20, verbose_name="预测 Regime")),
                (
                    "regime_actual",
                    models.CharField(
                        blank=True, max_length=20, null=True, verbose_name="实际 Regime"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "backtest",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="attribution_reports",
                        to="backtest.backtestresultmodel",
                        verbose_name="关联回测",
                    ),
                ),
            ],
            options={
                "verbose_name": "归因分析报告",
                "verbose_name_plural": "归因分析报告",
                "db_table": "audit_attribution_report",
                "ordering": ["-period_end"],
            },
        ),
        migrations.CreateModel(
            name="ExperienceSummary",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "lesson",
                    models.TextField(help_text="从本次回测中学到的教训", verbose_name="经验教训"),
                ),
                (
                    "recommendation",
                    models.TextField(help_text="针对性的改进措施", verbose_name="改进建议"),
                ),
                (
                    "priority",
                    models.CharField(
                        choices=[("HIGH", "高优先级"), ("MEDIUM", "中优先级"), ("LOW", "低优先级")],
                        default="MEDIUM",
                        max_length=20,
                        verbose_name="优先级",
                    ),
                ),
                ("is_applied", models.BooleanField(default=False, verbose_name="是否已应用")),
                (
                    "applied_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="应用时间"),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "report",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="experience_summaries",
                        to="audit.attributionreport",
                        verbose_name="归因报告",
                    ),
                ),
            ],
            options={
                "verbose_name": "经验总结",
                "verbose_name_plural": "经验总结",
                "db_table": "audit_experience_summary",
                "ordering": ["-priority", "-created_at"],
            },
        ),
        migrations.CreateModel(
            name="LossAnalysis",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "loss_source",
                    models.CharField(
                        choices=[
                            ("REGIME_ERROR", "Regime 判断错误"),
                            ("TIMING_ERROR", "择时错误"),
                            ("ASSET_SELECTION_ERROR", "资产选择错误"),
                            ("EXECUTION_ERROR", "执行误差"),
                            ("TRANSACTION_COST", "交易成本"),
                            ("POLICY_MISJUDGMENT", "Policy 误判"),
                            ("EXTERNAL_SHOCK", "外部冲击"),
                        ],
                        max_length=50,
                        verbose_name="损失来源",
                    ),
                ),
                (
                    "impact",
                    models.FloatField(help_text="该因素造成的收益/损失", verbose_name="影响金额"),
                ),
                (
                    "impact_percentage",
                    models.FloatField(help_text="占总损失的百分比", verbose_name="影响占比"),
                ),
                (
                    "description",
                    models.TextField(help_text="损失产生的具体原因和情境", verbose_name="详细描述"),
                ),
                ("improvement_suggestion", models.TextField(blank=True, verbose_name="改进建议")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "report",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="loss_analyses",
                        to="audit.attributionreport",
                        verbose_name="归因报告",
                    ),
                ),
            ],
            options={
                "verbose_name": "损失归因分析",
                "verbose_name_plural": "损失归因分析",
                "db_table": "audit_loss_analysis",
                "ordering": ["-impact"],
            },
        ),
    ]
