# Generated by Django 5.2.10 on 2026-03-04 08:35

import uuid

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


class Migration(migrations.Migration):

    dependencies = [
        ("account", "0013_accountprofilemodel_rbac_role"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="PortfolioObserverGrantModel",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, primary_key=True, serialize=False, verbose_name="授权ID"
                    ),
                ),
                (
                    "scope",
                    models.CharField(
                        choices=[("portfolio_read", "查看投资组合")],
                        default="portfolio_read",
                        max_length=50,
                        verbose_name="授权范围",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[("active", "激活"), ("revoked", "已撤销"), ("expired", "已过期")],
                        default="active",
                        max_length=20,
                        verbose_name="状态",
                    ),
                ),
                (
                    "expires_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="过期时间"),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                (
                    "revoked_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="撤销时间"),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_observer_grants",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="创建者",
                    ),
                ),
                (
                    "observer_user_id",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="observed_portfolios",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="观察员",
                    ),
                ),
                (
                    "owner_user_id",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="granted_observers",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="账户拥有者",
                    ),
                ),
                (
                    "revoked_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="revoked_observer_grants",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="撤销者",
                    ),
                ),
            ],
            options={
                "verbose_name": "投资组合观察员授权",
                "verbose_name_plural": "投资组合观察员授权",
                "db_table": "portfolio_observer_grant",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["owner_user_id", "observer_user_id"], name="idx_owner_observer"
                    ),
                    models.Index(fields=["observer_user_id", "status"], name="idx_observer_status"),
                    models.Index(
                        fields=["status", "expires_at"], name="portfolio_o_status_d5fa45_idx"
                    ),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        condition=models.Q(("status", "active")),
                        fields=("owner_user_id", "observer_user_id"),
                        name="unique_active_grant",
                    )
                ],
            },
        ),
    ]
