# Generated by Django 5.2.5 on 2025-08-22 20:02

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


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0005_alter_customuser_role_alter_rolepermission_role'),
    ]

    operations = [
        migrations.AddField(
            model_name='customuser',
            name='assigned_date',
            field=models.DateTimeField(blank=True, help_text='Date when client was assigned to portfolio manager', null=True),
        ),
        migrations.AddField(
            model_name='customuser',
            name='portfolio_manager',
            field=models.ForeignKey(blank=True, help_text="Loan officer or team leader managing this client's portfolio", limit_choices_to={'role__in': ['loan_officer', 'team_leader']}, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='portfolio_clients', to=settings.AUTH_USER_MODEL),
        ),
        migrations.CreateModel(
            name='PortfolioAssignment',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('assigned_date', models.DateTimeField(auto_now_add=True)),
                ('unassigned_date', models.DateTimeField(blank=True, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('reason', models.TextField(blank=True, help_text='Reason for assignment/reassignment', null=True)),
                ('assigned_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='assignments_made', to=settings.AUTH_USER_MODEL)),
                ('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='portfolio_history', to=settings.AUTH_USER_MODEL)),
                ('portfolio_manager', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='managed_assignments', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'portfolio_assignments',
                'ordering': ['-assigned_date'],
                'indexes': [models.Index(fields=['client', 'is_active'], name='portfolio_a_client__0ce7e4_idx'), models.Index(fields=['portfolio_manager', 'is_active'], name='portfolio_a_portfol_3ea06a_idx')],
            },
        ),
        migrations.CreateModel(
            name='PortfolioPerformance',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('month', models.DateField(help_text='First day of the month being tracked')),
                ('total_clients', models.PositiveIntegerField(default=0)),
                ('new_clients', models.PositiveIntegerField(default=0)),
                ('active_clients', models.PositiveIntegerField(default=0)),
                ('loans_disbursed', models.PositiveIntegerField(default=0)),
                ('total_disbursed_amount', models.DecimalField(decimal_places=2, default=0, max_digits=15)),
                ('loans_repaid', models.PositiveIntegerField(default=0)),
                ('total_repaid_amount', models.DecimalField(decimal_places=2, default=0, max_digits=15)),
                ('loans_defaulted', models.PositiveIntegerField(default=0)),
                ('total_defaulted_amount', models.DecimalField(decimal_places=2, default=0, max_digits=15)),
                ('collection_rate', models.DecimalField(decimal_places=2, default=0, help_text='Percentage', max_digits=5)),
                ('default_rate', models.DecimalField(decimal_places=2, default=0, help_text='Percentage', max_digits=5)),
                ('portfolio_at_risk', models.DecimalField(decimal_places=2, default=0, max_digits=15)),
                ('rank_by_disbursement', models.PositiveIntegerField(blank=True, null=True)),
                ('rank_by_collection', models.PositiveIntegerField(blank=True, null=True)),
                ('rank_by_clients', models.PositiveIntegerField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('portfolio_manager', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='performance_records', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'portfolio_performance',
                'ordering': ['-month', 'rank_by_disbursement'],
                'indexes': [models.Index(fields=['month', 'portfolio_manager'], name='portfolio_p_month_f7bf8b_idx'), models.Index(fields=['month', 'rank_by_disbursement'], name='portfolio_p_month_eb9039_idx')],
                'unique_together': {('portfolio_manager', 'month')},
            },
        ),
    ]
