# Generated manually for Grazuri schema compatibility
# Task 4.3: Update foreign key relationships

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 = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("loans", "0024_add_penalty_date_field"),
    ]

    operations = [
        # Create BureauRecord model
        migrations.CreateModel(
            name="BureauRecord",
            fields=[
                ("id", models.AutoField(primary_key=True, serialize=False)),
                ("bureau_name", models.CharField(blank=True, max_length=100, null=True)),
                ("record_date", models.DateTimeField(blank=True, null=True)),
                ("credit_score", models.IntegerField(blank=True, null=True)),
                ("report_data", models.TextField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "baccount",
                    models.ForeignKey(
                        db_column="baccount",
                        help_text="Reference to loan account",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="bureau_records",
                        to="loans.loan",
                    ),
                ),
                (
                    "borrower",
                    models.ForeignKey(
                        db_column="borrower",
                        help_text="Reference to borrower (CustomUser)",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="bureau_records",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Bureau Record",
                "verbose_name_plural": "Bureau Records",
                "db_table": "bureau_records",
                "ordering": ["-record_date"],
            },
        ),
        # Create LoanDisbursement model
        migrations.CreateModel(
            name="LoanDisbursement",
            fields=[
                ("id", models.AutoField(primary_key=True, serialize=False)),
                ("amount", models.DecimalField(decimal_places=2, max_digits=12)),
                ("disbursement_date", models.DateTimeField()),
                (
                    "disbursement_method",
                    models.CharField(
                        blank=True,
                        help_text="Method of disbursement (M-Pesa, Bank Transfer, etc.)",
                        max_length=50,
                        null=True,
                    ),
                ),
                ("reference_number", models.CharField(blank=True, max_length=100, null=True)),
                ("notes", models.TextField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "loan",
                    models.ForeignKey(
                        db_column="loan",
                        help_text="Reference to loan",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="disbursements",
                        to="loans.loan",
                    ),
                ),
            ],
            options={
                "verbose_name": "Loan Disbursement",
                "verbose_name_plural": "Loan Disbursements",
                "db_table": "loan_disbursements",
                "ordering": ["-disbursement_date"],
            },
        ),
        # Create LoanFee model
        migrations.CreateModel(
            name="LoanFee",
            fields=[
                ("id", models.AutoField(primary_key=True, serialize=False)),
                (
                    "fee_type",
                    models.CharField(
                        help_text="Type of fee (processing, late payment, etc.)",
                        max_length=50,
                    ),
                ),
                ("fee_name", models.CharField(max_length=100)),
                ("amount", models.DecimalField(decimal_places=2, max_digits=12)),
                ("is_paid", models.BooleanField(default=False)),
                ("paid_date", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "loan",
                    models.ForeignKey(
                        db_column="loan",
                        help_text="Reference to loan",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="fees",
                        to="loans.loan",
                    ),
                ),
            ],
            options={
                "verbose_name": "Loan Fee",
                "verbose_name_plural": "Loan Fees",
                "db_table": "loan_fees",
                "ordering": ["-created_at"],
            },
        ),
        # Create LoanGuarantor model
        migrations.CreateModel(
            name="LoanGuarantor",
            fields=[
                ("id", models.AutoField(primary_key=True, serialize=False)),
                ("guarantor_name", models.CharField(max_length=200)),
                ("guarantor_phone", models.CharField(max_length=17)),
                ("guarantor_email", models.EmailField(blank=True, max_length=254, null=True)),
                ("guarantor_id_number", models.CharField(blank=True, max_length=50, null=True)),
                ("relationship", 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)),
                (
                    "borrower",
                    models.ForeignKey(
                        db_column="borrower",
                        help_text="Reference to borrower (CustomUser)",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="guarantor_records",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Loan Guarantor",
                "verbose_name_plural": "Loan Guarantors",
                "db_table": "loan_guarantors",
                "ordering": ["-created_at"],
            },
        ),
        # Create LoanStatus model
        migrations.CreateModel(
            name="LoanStatus",
            fields=[
                ("id", models.AutoField(primary_key=True, serialize=False)),
                ("status", models.CharField(max_length=50)),
                ("status_date", models.DateTimeField(default=django.utils.timezone.now)),
                ("notes", models.TextField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "changed_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="loan_status_changes",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "loan",
                    models.ForeignKey(
                        db_column="loan",
                        help_text="Reference to loan",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="status_history",
                        to="loans.loan",
                    ),
                ),
            ],
            options={
                "verbose_name": "Loan Status",
                "verbose_name_plural": "Loan Statuses",
                "db_table": "loan_statuses",
                "ordering": ["-status_date"],
            },
        ),
    ]
