# Generated by Django 5.2.7 on 2025-11-03 01:33

import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0023_add_client_approval_fields'),
    ]

    operations = [
        migrations.CreateModel(
            name='PagePermission',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('page_name', models.CharField(help_text="Name of the page (e.g., 'loans', 'clients', 'reports')", max_length=100)),
                ('action_code', models.CharField(help_text="Unique code for the action (e.g., 'loans_create_application')", max_length=50)),
                ('action_name', models.CharField(help_text='Human-readable name for the action', max_length=200)),
                ('description', models.TextField(help_text='Detailed description of what this permission allows')),
                ('category', models.CharField(choices=[('view', 'View/Access'), ('create', 'Create/Add'), ('edit', 'Edit/Modify'), ('delete', 'Delete/Remove'), ('approve', 'Approve/Reject'), ('export', 'Export/Download'), ('manage', 'Manage/Configure'), ('process', 'Process/Execute')], help_text='Category of permission for grouping', max_length=50)),
                ('is_critical', models.BooleanField(default=False, help_text='Whether this is a critical permission requiring special approval')),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('required_permissions', models.ManyToManyField(blank=True, help_text='Other permissions required for this permission to be effective', to='users.pagepermission')),
            ],
            options={
                'db_table': 'page_permissions',
                'ordering': ['page_name', 'category', 'action_name'],
            },
        ),
        migrations.CreateModel(
            name='PortfolioSnapshot',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('snapshot_date', models.DateField(help_text='Date of this portfolio snapshot')),
                ('total_clients', models.IntegerField(default=0, help_text='Total number of clients in portfolio')),
                ('active_clients', models.IntegerField(default=0, help_text='Number of active clients')),
                ('new_clients', models.IntegerField(default=0, help_text='New clients added on this date')),
                ('churned_clients', models.IntegerField(default=0, help_text='Clients who churned on this date')),
                ('active_loans', models.IntegerField(default=0, help_text='Number of active loans')),
                ('total_loans', models.IntegerField(default=0, help_text='Total number of loans ever issued')),
                ('new_loans', models.IntegerField(default=0, help_text='New loans disbursed on this date')),
                ('completed_loans', models.IntegerField(default=0, help_text='Loans completed on this date')),
                ('defaulted_loans', models.IntegerField(default=0, help_text='Loans that defaulted on this date')),
                ('total_disbursed', models.DecimalField(decimal_places=2, default=0, help_text='Total amount disbursed to date', max_digits=15)),
                ('total_outstanding', models.DecimalField(decimal_places=2, default=0, help_text='Total outstanding amount', max_digits=15)),
                ('total_collected', models.DecimalField(decimal_places=2, default=0, help_text='Total amount collected to date', max_digits=15)),
                ('daily_disbursements', models.DecimalField(decimal_places=2, default=0, help_text='Amount disbursed on this date', max_digits=15)),
                ('daily_collections', models.DecimalField(decimal_places=2, default=0, help_text='Amount collected on this date', max_digits=15)),
                ('par_30', models.DecimalField(decimal_places=2, default=0, help_text='Portfolio at Risk (30+ days overdue)', max_digits=15)),
                ('par_60', models.DecimalField(decimal_places=2, default=0, help_text='Portfolio at Risk (60+ days overdue)', max_digits=15)),
                ('par_90', models.DecimalField(decimal_places=2, default=0, help_text='Portfolio at Risk (90+ days overdue)', max_digits=15)),
                ('default_rate', models.DecimalField(decimal_places=2, default=0, help_text='Default rate as percentage', max_digits=5)),
                ('collection_rate', models.DecimalField(decimal_places=2, default=0, help_text='Collection rate as percentage', max_digits=5)),
                ('portfolio_yield', models.DecimalField(decimal_places=2, default=0, help_text='Portfolio yield as percentage', max_digits=5)),
                ('average_loan_size', models.DecimalField(decimal_places=2, default=0, help_text='Average loan size', max_digits=12)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('branch', models.ForeignKey(help_text='Branch this snapshot belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='portfolio_snapshots', to='users.Branch')),
                ('manager', models.ForeignKey(help_text='Portfolio manager this snapshot belongs to', limit_choices_to={'role__in': ['loan_officer', 'team_leader']}, on_delete=django.db.models.deletion.CASCADE, related_name='portfolio_snapshots', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'portfolio_snapshots',
                'ordering': ['-snapshot_date', 'manager__username'],
            },
        ),
        migrations.CreateModel(
            name='RolePermissionTemplate',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('role', models.CharField(choices=[('admin', 'Admin'), ('team_leader', 'Team Leader'), ('loan_officer', 'Loan Officer'), ('secretary', 'Secretary'), ('auditor', 'Auditor'), ('borrower', 'Borrower')], help_text='Role this template applies to', max_length=20)),
                ('is_allowed', models.BooleanField(default=False, help_text='Whether this role has this permission by default')),
                ('can_override', models.BooleanField(default=True, help_text='Whether individual users can override this permission')),
                ('priority', models.IntegerField(default=0, help_text='Priority for permission resolution (higher = more important)')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_role_templates', to=settings.AUTH_USER_MODEL)),
                ('page_permission', models.ForeignKey(help_text='The specific page permission', on_delete=django.db.models.deletion.CASCADE, to='users.pagepermission')),
            ],
            options={
                'db_table': 'role_permission_templates',
                'ordering': ['role', 'page_permission__page_name', 'page_permission__category'],
            },
        ),
        migrations.CreateModel(
            name='UserPagePermission',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('is_allowed', models.BooleanField(help_text='Whether this user has this permission (overrides role default)')),
                ('reason', models.TextField(blank=True, help_text='Reason for granting this permission override')),
                ('expires_at', models.DateTimeField(blank=True, help_text='When this permission override expires (null = never expires)', null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('granted_by', models.ForeignKey(blank=True, help_text='User who granted this permission override', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='granted_page_permissions', to=settings.AUTH_USER_MODEL)),
                ('page_permission', models.ForeignKey(help_text='The specific page permission being overridden', on_delete=django.db.models.deletion.CASCADE, to='users.pagepermission')),
                ('user', models.ForeignKey(help_text='User this custom permission applies to', on_delete=django.db.models.deletion.CASCADE, related_name='custom_page_permissions', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'user_page_permissions',
                'ordering': ['user__username', 'page_permission__page_name', 'page_permission__category'],
            },
        ),
        migrations.CreateModel(
            name='ClientGrowthMetrics',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('period_start', models.DateField(help_text='Start date of the measurement period')),
                ('period_end', models.DateField(help_text='End date of the measurement period')),
                ('period_type', models.CharField(choices=[('daily', 'Daily'), ('weekly', 'Weekly'), ('monthly', 'Monthly'), ('quarterly', 'Quarterly'), ('yearly', 'Yearly')], default='monthly', help_text='Type of period this metric covers', max_length=20)),
                ('new_clients', models.IntegerField(default=0, help_text='Number of new clients acquired in this period')),
                ('churned_clients', models.IntegerField(default=0, help_text='Number of clients who churned in this period')),
                ('reactivated_clients', models.IntegerField(default=0, help_text='Number of previously inactive clients who became active')),
                ('total_clients', models.IntegerField(default=0, help_text='Total number of clients at end of period')),
                ('acquisition_cost', models.DecimalField(decimal_places=2, default=0, help_text='Average cost to acquire a new client', max_digits=10)),
                ('lifetime_value', models.DecimalField(decimal_places=2, default=0, help_text='Average lifetime value of clients acquired in this period', max_digits=15)),
                ('revenue_per_client', models.DecimalField(decimal_places=2, default=0, help_text='Average revenue per client in this period', max_digits=12)),
                ('total_revenue', models.DecimalField(decimal_places=2, default=0, help_text='Total revenue generated in this period', max_digits=15)),
                ('age_distribution', models.JSONField(blank=True, default=dict, help_text='Age distribution of new clients (JSON format)')),
                ('gender_distribution', models.JSONField(blank=True, default=dict, help_text='Gender distribution of new clients (JSON format)')),
                ('location_distribution', models.JSONField(blank=True, default=dict, help_text='Geographic distribution of new clients (JSON format)')),
                ('business_type_distribution', models.JSONField(blank=True, default=dict, help_text='Business type distribution of new clients (JSON format)')),
                ('top_performing_officers', models.JSONField(blank=True, default=list, help_text='List of top performing loan officers in this period')),
                ('officer_acquisition_stats', models.JSONField(blank=True, default=dict, help_text='Client acquisition statistics by loan officer')),
                ('approval_rate', models.DecimalField(decimal_places=2, default=0, help_text='Application approval rate as percentage', max_digits=5)),
                ('retention_rate', models.DecimalField(decimal_places=2, default=0, help_text='Client retention rate as percentage', max_digits=5)),
                ('satisfaction_score', models.DecimalField(decimal_places=1, default=0, help_text='Average client satisfaction score (0-10)', max_digits=3)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('branch', models.ForeignKey(help_text='Branch this metric belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='growth_metrics', to='users.Branch')),
                ('calculated_by', models.ForeignKey(blank=True, help_text='User who calculated/updated this metric', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='calculated_growth_metrics', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'client_growth_metrics',
                'ordering': ['-period_end', 'branch__name'],
                'indexes': [models.Index(fields=['branch', 'period_type'], name='client_grow_branch__767128_idx'), models.Index(fields=['period_start', 'period_end'], name='client_grow_period__ac6e65_idx'), models.Index(fields=['-period_end'], name='client_grow_period__ced6a3_idx'), models.Index(fields=['period_type'], name='client_grow_period__84869f_idx')],
                'unique_together': {('branch', 'period_start', 'period_end', 'period_type')},
            },
        ),
        migrations.AddIndex(
            model_name='pagepermission',
            index=models.Index(fields=['page_name', 'category'], name='page_permis_page_na_8fff83_idx'),
        ),
        migrations.AddIndex(
            model_name='pagepermission',
            index=models.Index(fields=['action_code'], name='page_permis_action__475561_idx'),
        ),
        migrations.AddIndex(
            model_name='pagepermission',
            index=models.Index(fields=['is_active'], name='page_permis_is_acti_ead259_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='pagepermission',
            unique_together={('page_name', 'action_code')},
        ),
        migrations.AddIndex(
            model_name='portfoliosnapshot',
            index=models.Index(fields=['manager', 'snapshot_date'], name='portfolio_s_manager_1734c3_idx'),
        ),
        migrations.AddIndex(
            model_name='portfoliosnapshot',
            index=models.Index(fields=['branch', 'snapshot_date'], name='portfolio_s_branch__523fba_idx'),
        ),
        migrations.AddIndex(
            model_name='portfoliosnapshot',
            index=models.Index(fields=['snapshot_date'], name='portfolio_s_snapsho_ed5790_idx'),
        ),
        migrations.AddIndex(
            model_name='portfoliosnapshot',
            index=models.Index(fields=['-snapshot_date'], name='portfolio_s_snapsho_9d3b93_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='portfoliosnapshot',
            unique_together={('manager', 'snapshot_date')},
        ),
        migrations.AddIndex(
            model_name='rolepermissiontemplate',
            index=models.Index(fields=['role'], name='role_permis_role_26fc77_idx'),
        ),
        migrations.AddIndex(
            model_name='rolepermissiontemplate',
            index=models.Index(fields=['is_allowed'], name='role_permis_is_allo_4b8e75_idx'),
        ),
        migrations.AddIndex(
            model_name='rolepermissiontemplate',
            index=models.Index(fields=['can_override'], name='role_permis_can_ove_8629fa_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='rolepermissiontemplate',
            unique_together={('role', 'page_permission')},
        ),
        migrations.AddIndex(
            model_name='userpagepermission',
            index=models.Index(fields=['user', 'page_permission'], name='user_page_p_user_id_87be16_idx'),
        ),
        migrations.AddIndex(
            model_name='userpagepermission',
            index=models.Index(fields=['expires_at'], name='user_page_p_expires_695b6f_idx'),
        ),
        migrations.AddIndex(
            model_name='userpagepermission',
            index=models.Index(fields=['is_allowed'], name='user_page_p_is_allo_5cae7a_idx'),
        ),
        migrations.AddIndex(
            model_name='userpagepermission',
            index=models.Index(fields=['granted_by'], name='user_page_p_granted_afd346_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='userpagepermission',
            unique_together={('user', 'page_permission')},
        ),
    ]
