# Generated by Django 4.2.20 on 2025-04-22 07:50

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('ib_commission', '0001_initial'),
        ('transactions', '0009_alter_transaction_approved_by_and_more'),
        ('users', '0014_alter_crmuser_groups_alter_crmuser_user_permissions_and_more'),
        ('customers', '0013_alter_customerauth_blocked_by_and_more'),
        ('accounts', '0006_alter_account_options'),
    ]

    operations = [
        migrations.CreateModel(
            name='AcquisitionRecord',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('referral_code_used', models.CharField(blank=True, max_length=50, null=True)),
                ('acquisition_type', models.CharField(choices=[('DIRECT', 'Direct Visit'), ('REFERRAL', 'Referral'), ('CAMPAIGN', 'Marketing Campaign'), ('AFFILIATE', 'Affiliate'), ('OTHER', 'Other Source')], default='DIRECT', max_length=20)),
                ('utm_source', models.CharField(blank=True, max_length=100, null=True)),
                ('utm_medium', models.CharField(blank=True, max_length=100, null=True)),
                ('utm_campaign', models.CharField(blank=True, max_length=100, null=True)),
                ('utm_content', models.CharField(blank=True, max_length=100, null=True)),
                ('utm_term', models.CharField(blank=True, max_length=100, null=True)),
                ('registration_channel', models.CharField(choices=[('WEB', 'Website'), ('MOBILE_APP', 'Mobile App'), ('DIRECT_API', 'API Registration'), ('MANUAL', 'Manually Created')], max_length=20)),
                ('registration_ip', models.GenericIPAddressField(blank=True, null=True)),
                ('landing_page', models.CharField(blank=True, max_length=255, null=True)),
                ('registration_date', models.DateTimeField(auto_now_add=True)),
                ('kyc_approved_date', models.DateTimeField(blank=True, null=True)),
                ('first_deposit_date', models.DateTimeField(blank=True, null=True)),
                ('first_trade_date', models.DateTimeField(blank=True, null=True)),
                ('journey_status', models.CharField(choices=[('REGISTERED', 'Registered'), ('KYC_PENDING', 'KYC Pending'), ('KYC_APPROVED', 'KYC Approved'), ('FUNDED', 'Account Funded'), ('TRADING', 'Actively Trading'), ('DORMANT', 'Dormant'), ('CHURNED', 'Churned')], default='REGISTERED', max_length=20)),
                ('assigned_agent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='assigned_acquisitions', to='users.crmuser')),
                ('customer', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='acquisition', to='customers.customer')),
            ],
            options={
                'verbose_name': 'Acquisition Record',
                'verbose_name_plural': 'Acquisition Records',
                'db_table': 'acquisition_records',
            },
        ),
        migrations.CreateModel(
            name='ReferralEvent',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('event_type', models.CharField(choices=[('REGISTRATION', 'Registration'), ('KYC_SUBMISSION', 'KYC Submitted'), ('KYC_APPROVED', 'KYC Approved'), ('FIRST_DEPOSIT', 'First Deposit'), ('FIRST_TRADE', 'First Trade'), ('CONVERSION', 'Conversion Milestone'), ('IB_ASSIGNMENT', 'IB Assignment'), ('AGENT_ASSIGNMENT', 'Agent Assignment')], max_length=20)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('data', models.JSONField(blank=True, default=dict)),
                ('is_eligible_for_reward', models.BooleanField(default=False)),
                ('reward_processed', models.BooleanField(default=False)),
                ('reward_amount', models.DecimalField(blank=True, decimal_places=5, max_digits=20, null=True)),
                ('acquisition', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='events', to='referrals.acquisitionrecord')),
            ],
            options={
                'verbose_name': 'Referral Event',
                'verbose_name_plural': 'Referral Events',
                'db_table': 'referral_events',
            },
        ),
        migrations.CreateModel(
            name='ReferralReward',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('name', models.CharField(max_length=100)),
                ('description', models.TextField(blank=True)),
                ('source_type_eligible', models.CharField(choices=[('AGENT', 'Sales Agent'), ('IB', 'Introducing Broker'), ('CLIENT', 'Regular Client'), ('AFFILIATE', 'Affiliate Partner'), ('ALL', 'All Sources')], default='ALL', max_length=20)),
                ('milestone', models.CharField(choices=[('REGISTRATION', 'Registration'), ('KYC_APPROVED', 'KYC Approved'), ('FIRST_DEPOSIT', 'First Deposit'), ('FIRST_TRADE', 'First Trade'), ('VOLUME_THRESHOLD', 'Trading Volume'), ('TIME_ACTIVE', 'Time as Active Client')], max_length=20)),
                ('reward_type', models.CharField(choices=[('FIXED', 'Fixed Amount'), ('PERCENTAGE', 'Percentage of Value'), ('TIER', 'Tiered Reward'), ('CUSTOM', 'Custom Reward')], max_length=20)),
                ('reward_amount', models.DecimalField(decimal_places=5, help_text='Default reward amount if type is not TIER, or fallback if no tier matches.', max_digits=20)),
                ('currency', models.CharField(default='USD', max_length=3)),
                ('tier_basis', models.CharField(blank=True, choices=[('DEPOSIT_AMOUNT', 'First Deposit Amount'), ('TRADE_VOLUME', 'First Trade Volume'), ('CONVERSION_COUNT', 'Number of Conversions'), ('REGISTRATION_COUNT', 'Number of Registrations')], help_text='Basis for tier calculation (e.g., deposit amount). Null if reward_type is not TIER.', max_length=20, null=True)),
                ('default_reward_type', models.CharField(blank=True, choices=[('FIXED', 'Fixed Amount'), ('PERCENTAGE', 'Percentage of Value'), ('TIER', 'Tiered Reward'), ('CUSTOM', 'Custom Reward')], help_text='Default reward type to use if no specific tier matches (optional).', max_length=20, null=True)),
                ('default_reward_amount', models.DecimalField(blank=True, decimal_places=5, help_text='Default reward amount to use if no specific tier matches (optional).', max_digits=20, null=True)),
                ('transaction_type', models.CharField(choices=[('DEPOSIT', 'Deposit'), ('WITHDRAWAL', 'Withdrawal'), ('BONUS', 'Bonus'), ('REBATE', 'Rebate'), ('COMMISSION', 'Commission'), ('CORRECTION', 'Correction'), ('INTERNAL_TRANSFER', 'Internal Transfer'), ('CREDIT', 'Credit'), ('CHARGE', 'Charge'), ('COMPENSATION', 'Compensation')], default='BONUS', help_text='Type of transaction to create when processing rewards (Bonus, Credit, Deposit, Compensation)', max_length=20)),
                ('trading_requirement_multiplier', models.DecimalField(decimal_places=2, default=0.0, help_text='Multiplier for trading volume requirement before withdrawal (e.g., 5x of bonus amount). 0 means no requirement.', max_digits=5)),
                ('withdrawal_lock_days', models.IntegerField(default=0, help_text='Number of days the reward is locked from withdrawal. 0 means no lock.')),
                ('payment_method', models.CharField(blank=True, choices=[('WIRE', 'Wire Transfer'), ('CRYPTO', 'Cryptocurrency'), ('CARD', 'Credit/Debit Card'), ('MANUAL', 'Manual'), ('SYSTEM', 'System Generated'), ('UPI', 'UPI Payment')], help_text='Specific payment method to simulate for deposit rewards (optional).', max_length=50, null=True)),
                ('payment_gateway', models.CharField(blank=True, choices=[('STRIPE', 'Stripe'), ('MYFATOORAH', 'MyFatoorah'), ('MANUAL', 'Manual Processing'), ('CRYPTO', 'Crypto Payment'), ('WIRE', 'Wire Transfer')], help_text='Specific payment gateway to simulate for deposit rewards (optional).', max_length=50, null=True)),
                ('reward_ib_directly', models.BooleanField(default=True, help_text='Should the direct IB referrer receive this reward?')),
                ('reward_ib_agent', models.BooleanField(default=False, help_text="Should the IB's assigned agent also receive a share of this reward?")),
                ('direct_referrer_share', models.DecimalField(decimal_places=2, default=100.0, help_text='Percentage share of the reward for the direct referrer (IB/Client/Agent).', max_digits=5)),
                ('ib_agent_share', models.DecimalField(decimal_places=2, default=0.0, help_text="Percentage share of the reward for the IB's agent (if reward_ib_agent is True).", max_digits=5)),
                ('minimum_deposit', models.DecimalField(decimal_places=5, default=0, help_text='Minimum first deposit amount required to trigger this reward.', max_digits=20)),
                ('minimum_volume', models.DecimalField(decimal_places=5, default=0, max_digits=20)),
                ('maximum_reward', models.DecimalField(blank=True, decimal_places=5, max_digits=20, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('valid_from', models.DateTimeField(blank=True, null=True)),
                ('valid_until', models.DateTimeField(blank=True, null=True)),
            ],
            options={
                'verbose_name': 'Referral Reward',
                'verbose_name_plural': 'Referral Rewards',
                'db_table': 'referral_rewards',
            },
        ),
        migrations.CreateModel(
            name='ReferralSource',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('source_type', models.CharField(choices=[('AGENT', 'Sales Agent'), ('IB', 'Introducing Broker'), ('CLIENT', 'Regular Client'), ('CAMPAIGN', 'Marketing Campaign'), ('AFFILIATE', 'Affiliate Partner')], max_length=20)),
                ('code', models.CharField(db_index=True, max_length=50, unique=True)),
                ('campaign', models.CharField(blank=True, max_length=100, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('total_referrals', models.IntegerField(default=0)),
                ('converted_referrals', models.IntegerField(default=0)),
                ('agreement', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_codes', to='ib_commission.ibagreement')),
                ('crm_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_codes', to='users.crmuser')),
                ('customer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_codes', to='customers.customer')),
                ('target_reward_account', models.ForeignKey(blank=True, help_text='Optional: Specific account to receive rewards generated by this code.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='reward_referral_sources', to='accounts.account')),
            ],
            options={
                'verbose_name': 'Referral Source',
                'verbose_name_plural': 'Referral Sources',
                'db_table': 'referral_sources',
            },
        ),
        migrations.CreateModel(
            name='RewardTier',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('min_value', models.DecimalField(decimal_places=5, help_text='Minimum value (inclusive) for this tier based on ReferralReward.tier_basis.', max_digits=20)),
                ('max_value', models.DecimalField(blank=True, decimal_places=5, help_text='Maximum value (exclusive) for this tier. Null means no upper limit.', max_digits=20, null=True)),
                ('reward_type', models.CharField(choices=[('FIXED', 'Fixed Amount'), ('PERCENTAGE', 'Percentage of Value'), ('TIER', 'Tiered Reward'), ('CUSTOM', 'Custom Reward')], default='FIXED', help_text='Type of reward for this specific tier (e.g., Fixed amount or Percentage).', max_length=20)),
                ('reward_amount', models.DecimalField(decimal_places=5, help_text='Reward amount or percentage for this tier.', max_digits=20)),
                ('order', models.PositiveIntegerField(default=0, help_text='Order in which tiers are evaluated.')),
                ('referral_reward', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tiers', to='referrals.referralreward')),
            ],
            options={
                'verbose_name': 'Reward Tier',
                'verbose_name_plural': 'Reward Tiers',
                'db_table': 'referral_reward_tiers',
                'ordering': ['referral_reward', 'order', 'min_value'],
            },
        ),
        migrations.CreateModel(
            name='RewardDistribution',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.CharField(blank=True, max_length=255, null=True)),
                ('updated_by', models.CharField(blank=True, max_length=255, null=True)),
                ('amount', models.DecimalField(decimal_places=5, max_digits=20)),
                ('currency', models.CharField(default='USD', max_length=3)),
                ('processing_status', models.CharField(choices=[('PENDING', 'Pending'), ('PROCESSED', 'Processed'), ('FAILED', 'Failed'), ('CANCELLED', 'Cancelled')], default='PENDING', max_length=20)),
                ('processed_date', models.DateTimeField(blank=True, null=True)),
                ('notes', models.TextField(blank=True)),
                ('acquisition', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='rewards', to='referrals.acquisitionrecord')),
                ('event', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='rewards', to='referrals.referralevent')),
                ('recipient_agent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_rewards', to='users.crmuser')),
                ('recipient_customer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_rewards', to='customers.customer')),
                ('referral_source', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='rewards', to='referrals.referralsource')),
                ('reward', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='distributions', to='referrals.referralreward')),
                ('transaction', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_rewards', to='transactions.transaction')),
            ],
            options={
                'verbose_name': 'Reward Distribution',
                'verbose_name_plural': 'Reward Distributions',
                'db_table': 'reward_distributions',
            },
        ),
        migrations.AddField(
            model_name='acquisitionrecord',
            name='referral_source',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='acquisitions', to='referrals.referralsource'),
        ),
        migrations.AddConstraint(
            model_name='referralsource',
            constraint=models.CheckConstraint(check=models.Q(models.Q(('crm_user__isnull', False), ('source_type', 'AGENT')), models.Q(('customer__isnull', False), ('source_type__in', ['IB', 'CLIENT'])), ('source_type__in', ['CAMPAIGN', 'AFFILIATE']), _connector='OR'), name='valid_referral_source_type'),
        ),
    ]
