# Generated by Django 5.2.6 on 2026-06-10 17:01

import django.db.models.deletion
import django.utils.timezone
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("organizations", "0002_workspace_template_user_context_pref"),
        ("tenants", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="PlatformPlan",
            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",
                    ),
                ),
                (
                    "code",
                    models.CharField(
                        db_index=True,
                        help_text="Stable code: free | starter | business | enterprise | custom",
                        max_length=40,
                        unique=True,
                        verbose_name="code",
                    ),
                ),
                ("name", models.CharField(max_length=200, verbose_name="name")),
                (
                    "description",
                    models.TextField(blank=True, default="", verbose_name="description"),
                ),
                (
                    "price_monthly",
                    models.DecimalField(
                        decimal_places=2, default=0, max_digits=12, verbose_name="monthly price"
                    ),
                ),
                (
                    "price_yearly",
                    models.DecimalField(
                        decimal_places=2, default=0, max_digits=12, verbose_name="yearly price"
                    ),
                ),
                (
                    "currency",
                    models.CharField(
                        choices=[("IRR", "Iranian Rial"), ("USD", "US Dollar")],
                        default="IRR",
                        max_length=8,
                        verbose_name="currency",
                    ),
                ),
                (
                    "user_limit",
                    models.IntegerField(
                        default=-1, help_text="-1 means unlimited", verbose_name="user limit"
                    ),
                ),
                ("storage_gb", models.IntegerField(default=1, verbose_name="storage (GB)")),
                (
                    "workflow_limit",
                    models.IntegerField(
                        default=-1, help_text="-1 means unlimited", verbose_name="workflow limit"
                    ),
                ),
                (
                    "api_calls_per_month",
                    models.IntegerField(
                        default=-1,
                        help_text="-1 means unlimited",
                        verbose_name="API calls per month",
                    ),
                ),
                ("trial_days", models.IntegerField(default=14, verbose_name="trial days")),
                (
                    "is_public",
                    models.BooleanField(
                        default=True, help_text="Show on pricing page", verbose_name="public"
                    ),
                ),
                ("is_active", models.BooleanField(default=True, verbose_name="active")),
                (
                    "sort_order",
                    models.PositiveIntegerField(
                        db_index=True, default=0, verbose_name="sort order"
                    ),
                ),
            ],
            options={
                "verbose_name": "platform plan",
                "verbose_name_plural": "platform plans",
                "ordering": ("sort_order", "code"),
            },
        ),
        migrations.CreateModel(
            name="PlanFeature",
            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_key",
                    models.CharField(
                        db_index=True,
                        help_text="Dotted key, e.g. 'crm.pipeline'",
                        max_length=120,
                        verbose_name="feature key",
                    ),
                ),
                (
                    "enabled_by_default",
                    models.BooleanField(default=True, verbose_name="enabled by default"),
                ),
                (
                    "config_json",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Limits, e.g. {'max': 100}",
                        verbose_name="config",
                    ),
                ),
                (
                    "plan",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="plan_features",
                        to="subscription.platformplan",
                        verbose_name="plan",
                    ),
                ),
            ],
            options={
                "verbose_name": "plan feature",
                "verbose_name_plural": "plan features",
                "ordering": ("plan__code", "feature_key"),
                "constraints": [
                    models.UniqueConstraint(
                        fields=("plan", "feature_key"),
                        name="subscription_planfeature_unique_plan_key",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="TenantAddonFeature",
            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_key",
                    models.CharField(
                        db_index=True,
                        help_text="Dotted key, e.g. 'ai.assistant'",
                        max_length=120,
                        verbose_name="feature key",
                    ),
                ),
                (
                    "purchased_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now, verbose_name="purchased at"
                    ),
                ),
                (
                    "expires_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="null = never expires",
                        null=True,
                        verbose_name="expires at",
                    ),
                ),
                (
                    "price_paid",
                    models.DecimalField(
                        decimal_places=2, default=0, max_digits=12, verbose_name="price paid"
                    ),
                ),
                (
                    "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",
                    ),
                ),
            ],
            options={
                "verbose_name": "tenant addon feature",
                "verbose_name_plural": "tenant addon features",
                "ordering": ("-purchased_at",),
                "indexes": [
                    models.Index(
                        fields=["tenant", "feature_key"], name="subscriptio_tenant__2f3f19_idx"
                    )
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("tenant", "feature_key"),
                        name="subscription_addonfeature_unique_per_tenant",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="TenantSubscription",
            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",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("trial", "Trial"),
                            ("active", "Active"),
                            ("suspended", "Suspended"),
                            ("expired", "Expired"),
                            ("cancelled", "Cancelled"),
                        ],
                        db_index=True,
                        default="trial",
                        max_length=16,
                        verbose_name="status",
                    ),
                ),
                (
                    "trial_ends_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="trial ends at"),
                ),
                (
                    "current_period_start",
                    models.DateTimeField(blank=True, null=True, verbose_name="period start"),
                ),
                (
                    "current_period_end",
                    models.DateTimeField(blank=True, null=True, verbose_name="period end"),
                ),
                ("seat_count", models.IntegerField(default=1, verbose_name="seat count")),
                (
                    "cancelled_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="cancelled at"),
                ),
                (
                    "cancellation_reason",
                    models.TextField(blank=True, default="", verbose_name="cancellation reason"),
                ),
                (
                    "organization_node",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="organizations.organizationnode",
                        verbose_name="organization node",
                    ),
                ),
                (
                    "plan",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="subscriptions",
                        to="subscription.platformplan",
                        verbose_name="plan",
                    ),
                ),
                (
                    "tenant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to="tenants.tenant",
                        verbose_name="tenant",
                    ),
                ),
            ],
            options={
                "verbose_name": "tenant subscription",
                "verbose_name_plural": "tenant subscriptions",
                "ordering": ("-created_at",),
                "indexes": [
                    models.Index(fields=["tenant", "status"], name="subscriptio_tenant__76182f_idx")
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("tenant",), name="subscription_tenantsub_unique_per_tenant"
                    )
                ],
            },
        ),
    ]
