from decimal import Decimal
import uuid
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
        ('products', '0011_add_hsn_code_to_product'),
        ('distributor', '0011_rename_dist_binary_parent_pos_idx_distributor_binary__eaaffb_idx'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='distributorid',
            name='binary_level_depth',
            field=models.PositiveIntegerField(db_index=True, default=0),
        ),
        migrations.AddField(
            model_name='distributorid',
            name='binary_path',
            field=models.CharField(blank=True, db_index=True, default='', max_length=1024),
        ),
        migrations.AddField(
            model_name='distributorid',
            name='binary_subtree_filled_count',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='distributorid',
            name='is_company_root',
            field=models.BooleanField(db_index=True, default=False),
        ),
        migrations.CreateModel(
            name='DistributorOwnershipPeriod',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('ownership_effective_from', models.DateTimeField(db_index=True)),
                ('ownership_effective_to', models.DateTimeField(blank=True, db_index=True, null=True)),
                ('transfer_reward_boundary_timestamp', models.DateTimeField(blank=True, db_index=True, null=True)),
                ('is_current', models.BooleanField(db_index=True, default=True)),
                ('direct_recruits_count', models.PositiveIntegerField(default=0)),
                ('withdrawal_unlocked', models.BooleanField(db_index=True, default=False)),
                ('power_stream_activated', models.BooleanField(db_index=True, default=False)),
                ('distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ownership_periods', to='distributor.distributorid')),
                ('owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='owned_distributor_periods', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='DistributorTransferTerms',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('title', models.CharField(max_length=255)),
                ('body', models.TextField()),
                ('display_order', models.PositiveIntegerField(default=1)),
                ('mandatory', models.BooleanField(default=True)),
                ('applies_from', models.DateTimeField(blank=True, null=True)),
                ('applies_to', models.DateTimeField(blank=True, null=True)),
            ],
            options={
                'ordering': ['display_order', 'created_at'],
            },
        ),
        migrations.CreateModel(
            name='AutoIDAllocation',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('level_number', models.PositiveIntegerField(db_index=True)),
                ('total_count', models.PositiveIntegerField(default=0)),
                ('consumed_count', models.PositiveIntegerField(default=0)),
                ('remaining_count', models.PositiveIntegerField(db_index=True, default=0)),
                ('auto_id_balance', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=14)),
                ('per_auto_id_value', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('status', models.CharField(choices=[('PENDING', 'Pending'), ('PARTIAL', 'Partially Consumed'), ('CONSUMED', 'Consumed')], db_index=True, default='PENDING', max_length=16)),
                ('eligible_product', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='auto_id_allocations', to='products.product')),
                ('owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auto_id_allocations', to=settings.AUTH_USER_MODEL)),
                ('source_distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auto_id_allocations', to='distributor.distributorid')),
            ],
        ),
        migrations.CreateModel(
            name='DistributorTransferRequest',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('referral_code', models.CharField(blank=True, default='', max_length=64)),
                ('reason', models.TextField(blank=True, default='')),
                ('status', models.CharField(choices=[('SUBMITTED', 'Submitted'), ('OLD_OWNER_CONFIRMED', 'Old Owner Confirmed'), ('APPROVED', 'Approved'), ('REJECTED', 'Rejected'), ('CANCELLED', 'Cancelled')], db_index=True, default='SUBMITTED', max_length=24)),
                ('reviewed_at', models.DateTimeField(blank=True, null=True)),
                ('admin_notes', models.TextField(blank=True, default='')),
                ('old_owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_requests_as_old_owner', to=settings.AUTH_USER_MODEL)),
                ('requested_new_owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_requests_as_new_owner', to=settings.AUTH_USER_MODEL)),
                ('reviewed_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='reviewed_transfer_requests', to=settings.AUTH_USER_MODEL)),
                ('target_distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_requests', to='distributor.distributorid')),
            ],
        ),
        migrations.CreateModel(
            name='DistributorTransferAcceptance',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('accepted', models.BooleanField(default=False)),
                ('accepted_at', models.DateTimeField(blank=True, null=True)),
                ('accepted_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='accepted_transfer_terms', to=settings.AUTH_USER_MODEL)),
                ('terms', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='acceptances', to='distributor.distributortransferterms')),
                ('transfer_request', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='acceptances', to='distributor.distributortransferrequest')),
            ],
        ),
        migrations.CreateModel(
            name='DistributorTransferHistory',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('approved_at', models.DateTimeField(db_index=True)),
                ('transfer_reward_boundary_timestamp', models.DateTimeField(db_index=True)),
                ('distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_histories', to='distributor.distributorid')),
                ('new_owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_histories_as_new_owner', to=settings.AUTH_USER_MODEL)),
                ('old_owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transfer_histories_as_old_owner', to=settings.AUTH_USER_MODEL)),
                ('transfer_request', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='history', to='distributor.distributortransferrequest')),
            ],
        ),
        migrations.CreateModel(
            name='DistributorQualificationState',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('required_directs', models.PositiveIntegerField(default=2)),
                ('achieved_directs', models.PositiveIntegerField(default=0)),
                ('withdrawal_eligible', models.BooleanField(db_index=True, default=False)),
                ('qualified_at', models.DateTimeField(blank=True, null=True)),
                ('distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='qualification_states', to='distributor.distributorid')),
                ('owner_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='distributor_qualification_states', to=settings.AUTH_USER_MODEL)),
                ('ownership_period', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='qualification_states', to='distributor.distributorownershipperiod')),
            ],
        ),
        migrations.CreateModel(
            name='DistributorRewardOwnershipLedger',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('reward_type', models.CharField(choices=[('SPONSOR_INCOME', 'Sponsor Income'), ('POWER_STREAM', 'Power Stream'), ('LEVEL_REWARD', 'Level Reward'), ('WITHDRAWAL', 'Withdrawal')], db_index=True, max_length=24)),
                ('reward_reference', models.CharField(db_index=True, max_length=128)),
                ('amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=14)),
                ('reward_timestamp', models.DateTimeField(db_index=True)),
                ('metadata', models.JSONField(blank=True, null=True)),
                ('beneficiary_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='distributor_reward_ownership_entries', to=settings.AUTH_USER_MODEL)),
                ('distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reward_ownership_ledger', to='distributor.distributorid')),
                ('ownership_period', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reward_entries', to='distributor.distributorownershipperiod')),
            ],
        ),
        migrations.CreateModel(
            name='AutoIDActivationLog',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('activation_cost', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('notes', models.TextField(blank=True, default='')),
                ('activated_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auto_id_activation_logs', to=settings.AUTH_USER_MODEL)),
                ('activated_distributor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auto_id_activation_logs', to='distributor.distributorid')),
                ('allocation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='activation_logs', to='distributor.autoidallocation')),
                ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auto_id_activation_logs', to='products.product')),
            ],
        ),
        migrations.AddConstraint(
            model_name='distributorqualificationstate',
            constraint=models.UniqueConstraint(fields=('ownership_period',), name='uniq_qualification_per_ownership_period'),
        ),
        migrations.AddConstraint(
            model_name='distributortransferacceptance',
            constraint=models.UniqueConstraint(fields=('transfer_request', 'terms', 'accepted_by'), name='uniq_transfer_term_acceptance'),
        ),
        migrations.AddIndex(
            model_name='autoidallocation',
            index=models.Index(fields=['owner_user', 'status'], name='distributor_owner_u_65e413_idx'),
        ),
        migrations.AddIndex(
            model_name='autoidallocation',
            index=models.Index(fields=['source_distributor', 'status'], name='distributor_source__912b07_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorownershipperiod',
            index=models.Index(fields=['distributor', 'is_current'], name='distributor_distrib_c4f899_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorownershipperiod',
            index=models.Index(fields=['owner_user', 'is_current'], name='distributor_owner_u_93cbcc_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorownershipperiod',
            index=models.Index(fields=['ownership_effective_from', 'ownership_effective_to'], name='distributor_ownersh_32defb_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorqualificationstate',
            index=models.Index(fields=['distributor', 'owner_user'], name='distributor_distrib_5f19f8_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorqualificationstate',
            index=models.Index(fields=['withdrawal_eligible'], name='distributor_withdra_b9ca82_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorrewardownershipledger',
            index=models.Index(fields=['distributor', 'reward_timestamp'], name='distributor_distrib_c4e720_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorrewardownershipledger',
            index=models.Index(fields=['beneficiary_user', 'reward_timestamp'], name='distributor_benefic_681034_idx'),
        ),
        migrations.AddIndex(
            model_name='distributorrewardownershipledger',
            index=models.Index(fields=['reward_type', 'reward_timestamp'], name='distributor_reward__390e69_idx'),
        ),
        migrations.AddIndex(
            model_name='distributortransferacceptance',
            index=models.Index(fields=['transfer_request', 'accepted_by'], name='distributor_transfe_df8a3a_idx'),
        ),
        migrations.AddIndex(
            model_name='distributortransferrequest',
            index=models.Index(fields=['target_distributor', 'status'], name='distributor_target__06ede2_idx'),
        ),
        migrations.AddIndex(
            model_name='distributortransferrequest',
            index=models.Index(fields=['requested_new_owner_user', 'status'], name='distributor_request_beb563_idx'),
        ),
        migrations.AddIndex(
            model_name='distributortransferrequest',
            index=models.Index(fields=['old_owner_user', 'status'], name='distributor_old_own_7870fd_idx'),
        ),
    ]
