# Generated by Django 5.2.7 on 2025-12-01 07:21

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


class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('django_rebac', '0003_resourcenode'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='HierarchyTypeDefinition',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('tenant_object_id', models.CharField(help_text='Primary key of the tenant', max_length=128)),
                ('name', models.CharField(help_text="Internal name (e.g., 'region', 'branch')", max_length=128)),
                ('display_name', models.CharField(help_text="Human-readable name (e.g., 'Region', 'Branch')", max_length=255)),
                ('slug', models.SlugField(help_text='URL-safe identifier', max_length=128)),
                ('level', models.PositiveIntegerField(default=0, help_text='Position in hierarchy (0 = root level)')),
                ('relations', models.JSONField(blank=True, default=dict, help_text='Relation definitions, e.g., {"manager": "user", "viewer": "user"}')),
                ('permissions', models.JSONField(blank=True, default=dict, help_text='Permission definitions, e.g., {"manage": "manager + parent->manage"}')),
                ('icon', models.CharField(blank=True, help_text='Icon identifier for UI', max_length=64)),
                ('color', models.CharField(blank=True, help_text='Color code for UI', max_length=32)),
                ('metadata_schema', models.JSONField(blank=True, default=dict, help_text='JSON schema for node metadata validation')),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('parent_type', models.ForeignKey(blank=True, help_text='What type can be a parent (null = root type)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='child_types', to='django_rebac.hierarchytypedefinition')),
                ('tenant_content_type', models.ForeignKey(help_text='Content type of the tenant model', on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
            ],
            options={
                'verbose_name': 'Hierarchy type definition',
                'verbose_name_plural': 'Hierarchy type definitions',
                'ordering': ('tenant_content_type', 'tenant_object_id', 'level', 'name'),
            },
        ),
        migrations.CreateModel(
            name='HierarchyNode',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('tenant_object_id', models.CharField(max_length=128)),
                ('name', models.CharField(max_length=255)),
                ('slug', models.SlugField(blank=True, max_length=255)),
                ('code', models.CharField(blank=True, help_text='Optional external code/identifier', max_length=64)),
                ('path', models.CharField(blank=True, db_index=True, help_text="Materialized path (e.g., '/1/5/12/')", max_length=1024)),
                ('depth', models.PositiveIntegerField(default=0, help_text='Depth in the hierarchy (0 = root)')),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='django_rebac.hierarchynode')),
                ('tenant_content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
                ('hierarchy_type', models.ForeignKey(help_text='The type of this node (Region, Branch, etc.)', on_delete=django.db.models.deletion.PROTECT, related_name='nodes', to='django_rebac.hierarchytypedefinition')),
            ],
            options={
                'verbose_name': 'Hierarchy node',
                'verbose_name_plural': 'Hierarchy nodes',
                'ordering': ('tenant_content_type', 'tenant_object_id', 'path', 'name'),
            },
        ),
        migrations.CreateModel(
            name='HierarchyNodeRole',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('role', models.CharField(choices=[('manager', 'Manager'), ('viewer', 'Viewer'), ('admin', 'Admin')], default='manager', max_length=64)),
                ('inheritable', models.BooleanField(default=True, help_text='Whether this role propagates to child nodes via parent->permission')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
                ('node', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='role_assignments', to='django_rebac.hierarchynode')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='hierarchy_role_assignments', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Hierarchy node role',
                'verbose_name_plural': 'Hierarchy node roles',
                'constraints': [models.UniqueConstraint(fields=('node', 'user', 'role'), name='rebac_unique_hierarchy_role')],
            },
        ),
        migrations.AddIndex(
            model_name='hierarchytypedefinition',
            index=models.Index(fields=['tenant_content_type', 'tenant_object_id'], name='django_reba_tenant__b4c7e8_idx'),
        ),
        migrations.AddConstraint(
            model_name='hierarchytypedefinition',
            constraint=models.UniqueConstraint(fields=('tenant_content_type', 'tenant_object_id', 'slug'), name='rebac_unique_hierarchy_type_slug'),
        ),
        migrations.AddIndex(
            model_name='hierarchynode',
            index=models.Index(fields=['tenant_content_type', 'tenant_object_id'], name='django_reba_tenant__23ec49_idx'),
        ),
        migrations.AddIndex(
            model_name='hierarchynode',
            index=models.Index(fields=['tenant_content_type', 'tenant_object_id', 'path'], name='django_reba_tenant__218145_idx'),
        ),
        migrations.AddIndex(
            model_name='hierarchynode',
            index=models.Index(fields=['tenant_content_type', 'tenant_object_id', 'hierarchy_type'], name='django_reba_tenant__585204_idx'),
        ),
        migrations.AddConstraint(
            model_name='hierarchynode',
            constraint=models.UniqueConstraint(condition=models.Q(('slug__gt', '')), fields=('tenant_content_type', 'tenant_object_id', 'slug'), name='rebac_unique_hierarchy_node_slug'),
        ),
    ]
