"""Initial migration for apps.business.binary_tree."""

import uuid
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('distributor', '0010_distributorid_binary_fields'),
        ('power_stream', '__first__'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        # ── BinaryTreeMetrics ──────────────────────────────────────────────
        migrations.CreateModel(
            name='BinaryTreeMetrics',
            fields=[
                ('distributor', models.OneToOneField(
                    on_delete=django.db.models.deletion.CASCADE,
                    primary_key=True, serialize=False,
                    related_name='binary_metrics',
                    to='distributor.distributorid',
                )),
                ('left_volume', models.PositiveIntegerField(default=0)),
                ('right_volume', models.PositiveIntegerField(default=0)),
                ('subtree_total', models.PositiveIntegerField(default=0)),
                ('direct_left_count', models.PositiveIntegerField(default=0)),
                ('direct_right_count', models.PositiveIntegerField(default=0)),
                ('tree_depth', models.PositiveIntegerField(default=0)),
                ('weak_leg', models.CharField(blank=True, default='', max_length=8,
                    choices=[('LEFT', 'Left'), ('RIGHT', 'Right'), ('BALANCED', 'Balanced')])),
                ('strong_leg', models.CharField(blank=True, default='', max_length=8,
                    choices=[('LEFT', 'Left'), ('RIGHT', 'Right'), ('BALANCED', 'Balanced')])),
                ('imbalance_ratio', models.DecimalField(decimal_places=2, default=0, max_digits=5)),
                ('last_rebuilt_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Binary Tree Metrics',
                'verbose_name_plural': 'Binary Tree Metrics',
            },
        ),
        migrations.AddIndex(
            model_name='binarytreemetrics',
            index=models.Index(fields=['subtree_total'], name='btmetrics_subtree_idx'),
        ),
        migrations.AddIndex(
            model_name='binarytreemetrics',
            index=models.Index(fields=['weak_leg'], name='btmetrics_weakleg_idx'),
        ),
        migrations.AddIndex(
            model_name='binarytreemetrics',
            index=models.Index(fields=['last_rebuilt_at'], name='btmetrics_rebuilt_idx'),
        ),

        # ── BinaryPlacementLog ─────────────────────────────────────────────
        migrations.CreateModel(
            name='BinaryPlacementLog',
            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)),
                ('distributor', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='placement_logs',
                    to='distributor.distributorid',
                )),
                ('binary_parent', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='placement_parent_logs',
                    to='distributor.distributorid',
                )),
                ('binary_position', models.CharField(db_index=True, max_length=5,
                    choices=[('LEFT', 'Left'), ('RIGHT', 'Right')])),
                ('placement_type', models.CharField(db_index=True, max_length=10,
                    choices=[('DIRECT', 'Direct'), ('SPILLOVER', 'Spillover')])),
                ('sponsor', models.ForeignKey(
                    blank=True, null=True,
                    on_delete=django.db.models.deletion.SET_NULL,
                    related_name='sponsored_placements',
                    to='distributor.distributorid',
                )),
                ('notes', models.TextField(blank=True, default='')),
            ],
            options={'ordering': ['-created_at']},
        ),
        migrations.AddIndex(
            model_name='binaryplacementlog',
            index=models.Index(fields=['distributor'], name='bpl_distributor_idx'),
        ),
        migrations.AddIndex(
            model_name='binaryplacementlog',
            index=models.Index(fields=['binary_parent', 'binary_position'], name='bpl_parent_pos_idx'),
        ),
        migrations.AddIndex(
            model_name='binaryplacementlog',
            index=models.Index(fields=['created_at'], name='bpl_created_idx'),
        ),

        # ── BinaryLevelSnapshot ────────────────────────────────────────────
        migrations.CreateModel(
            name='BinaryLevelSnapshot',
            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)),
                ('distributor', models.OneToOneField(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='binary_level_snapshot',
                    to='distributor.distributorid',
                )),
                ('subtree_total', models.PositiveIntegerField(default=0)),
                ('left_volume', models.PositiveIntegerField(default=0)),
                ('right_volume', models.PositiveIntegerField(default=0)),
                ('current_level', models.PositiveIntegerField(default=0)),
                ('last_completed_level', models.PositiveIntegerField(default=0)),
                ('next_level', models.PositiveIntegerField(blank=True, null=True)),
                ('next_level_required', models.PositiveIntegerField(blank=True, null=True)),
                ('positions_to_next', models.PositiveIntegerField(default=0)),
                ('progress_percent', models.DecimalField(decimal_places=1, default=0, max_digits=5)),
                ('calculated_at', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.AddIndex(
            model_name='binarylevelsnapshot',
            index=models.Index(fields=['current_level'], name='bls_level_idx'),
        ),
        migrations.AddIndex(
            model_name='binarylevelsnapshot',
            index=models.Index(fields=['calculated_at'], name='bls_calc_idx'),
        ),

        # ── PowerStreamForfeitureLog ───────────────────────────────────────
        migrations.CreateModel(
            name='PowerStreamForfeitureLog',
            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)),
                ('bonus_record', models.OneToOneField(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='forfeiture_log',
                    to='power_stream.powerstreambonusrecord',
                )),
                ('user', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='forfeiture_logs',
                    to=settings.AUTH_USER_MODEL,
                )),
                ('gross_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('net_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('forfeited_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('reason', models.TextField(blank=True, default='')),
                ('grace_expired', models.BooleanField(default=False)),
            ],
            options={'ordering': ['-forfeited_at']},
        ),
        migrations.AddIndex(
            model_name='powerstreamforfeiturelog',
            index=models.Index(fields=['user', 'forfeited_at'], name='psfl_user_date_idx'),
        ),
        migrations.AddIndex(
            model_name='powerstreamforfeiturelog',
            index=models.Index(fields=['forfeited_at'], name='psfl_date_idx'),
        ),
    ]
