# Generated by Django 5.2.11 on 2026-02-11 17:02

import django.core.validators
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('tenant', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='OrganizationalUnit',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(help_text='Display name of the organizational unit', max_length=255)),
                ('code', models.CharField(help_text="Unique code within tenant (e.g., 'HQ', 'SALES-01', 'IT-DEV')", max_length=50, validators=[django.core.validators.RegexValidator(message='Code must be uppercase alphanumeric with underscores or hyphens', regex='^[A-Z0-9_-]+$')])),
                ('description', models.TextField(blank=True, help_text='Optional description')),
                ('unit_type', models.CharField(choices=[('COMPANY', 'Company'), ('BRANCH', 'Branch/Location'), ('DIVISION', 'Division'), ('DEPARTMENT', 'Department'), ('TEAM', 'Team'), ('PROJECT', 'Project'), ('COST_CENTER', 'Cost Center'), ('CUSTOM', 'Custom')], db_index=True, default='DEPARTMENT', help_text='Type of organizational unit', max_length=50)),
                ('custom_type_name', models.CharField(blank=True, help_text='Custom type name when unit_type is CUSTOM', max_length=100)),
                ('path', models.CharField(blank=True, db_index=True, help_text='Materialized path for tree queries', max_length=1000)),
                ('level', models.PositiveIntegerField(db_index=True, default=0, help_text='Depth level in hierarchy (0 = root)')),
                ('sort_order', models.PositiveIntegerField(default=0, help_text='Sort order among siblings')),
                ('is_active', models.BooleanField(db_index=True, default=True, help_text='Whether this unit is active')),
                ('metadata', models.JSONField(blank=True, default=dict, help_text='Module-specific metadata (JSON)')),
                ('address', models.TextField(blank=True)),
                ('phone', models.CharField(blank=True, max_length=50)),
                ('email', models.EmailField(blank=True, max_length=254)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('parent', models.ForeignKey(blank=True, help_text='Parent organizational unit', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='core_organization.organizationalunit')),
                ('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='tenant.tenant')),
            ],
            options={
                'verbose_name': 'Organizational Unit',
                'verbose_name_plural': 'Organizational Units',
                'db_table': 'organizational_units',
                'ordering': ['path', 'sort_order', 'name'],
            },
        ),
        migrations.CreateModel(
            name='UserOrganizationalUnit',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('is_primary', models.BooleanField(default=False, help_text="Is this the user's primary organizational unit?")),
                ('is_manager', models.BooleanField(default=False, help_text='Is the user a manager of this unit?')),
                ('include_descendants', models.BooleanField(default=False, help_text='Does user have access to descendant units?')),
                ('start_date', models.DateField(blank=True, help_text='When user joined this unit', null=True)),
                ('end_date', models.DateField(blank=True, help_text='When user left/will leave this unit', null=True)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organizational_unit', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_memberships', to='core_organization.organizationalunit')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='organizational_units', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'User Organizational Unit',
                'verbose_name_plural': 'User Organizational Units',
                'db_table': 'user_organizational_units',
            },
        ),
        migrations.AddIndex(
            model_name='organizationalunit',
            index=models.Index(fields=['tenant', 'unit_type'], name='organizatio_tenant__f3abd7_idx'),
        ),
        migrations.AddIndex(
            model_name='organizationalunit',
            index=models.Index(fields=['tenant', 'is_active'], name='organizatio_tenant__2af6d0_idx'),
        ),
        migrations.AddIndex(
            model_name='organizationalunit',
            index=models.Index(fields=['tenant', 'parent'], name='organizatio_tenant__8e3c9e_idx'),
        ),
        migrations.AddIndex(
            model_name='organizationalunit',
            index=models.Index(fields=['path'], name='organizatio_path_4c9b3c_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='organizationalunit',
            unique_together={('tenant', 'code')},
        ),
        migrations.AlterUniqueTogether(
            name='userorganizationalunit',
            unique_together={('user', 'organizational_unit')},
        ),
    ]
