# Generated by Django 5.1.3 on 2026-05-23 10:32

import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("organizations", "0001_initial"),
        ("platform_modules", "0001_initial"),
        ("tenants", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="FeatureEntitlement",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                ("feature_code", models.CharField(max_length=64, verbose_name="feature")),
                (
                    "source",
                    models.CharField(
                        choices=[
                            ("included", "Included"),
                            ("purchased", "Purchased"),
                            ("trial", "Trial"),
                            ("granted", "Granted"),
                        ],
                        default="included",
                        max_length=16,
                        verbose_name="source",
                    ),
                ),
                ("enabled", models.BooleanField(default=True, verbose_name="enabled")),
                (
                    "starts_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="starts at"),
                ),
                (
                    "expires_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="expires at"),
                ),
                (
                    "revoked_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="revoked at"),
                ),
                ("metadata", models.JSONField(blank=True, default=dict, verbose_name="metadata")),
                (
                    "module",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="entitlements",
                        to="platform_modules.module",
                    ),
                ),
                (
                    "organization_node",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="organizations.organizationnode",
                        verbose_name="organization node",
                    ),
                ),
                (
                    "tenant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to="tenants.tenant",
                        verbose_name="tenant",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        help_text="Per-user entitlement; null means tenant-wide.",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "feature entitlement",
                "verbose_name_plural": "feature entitlements",
                "ordering": ("module__name", "feature_code"),
                "indexes": [
                    models.Index(
                        fields=["tenant", "module", "feature_code"],
                        name="platform_mo_tenant__cbddff_idx",
                    ),
                    models.Index(
                        fields=["tenant", "user", "revoked_at"],
                        name="platform_mo_tenant__c317d4_idx",
                    ),
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("tenant", "module", "feature_code", "user"),
                        name="modules_feature_entitlement_unique_scope",
                    )
                ],
            },
        ),
    ]
