# Generated by Django 5.2.13 on 2026-04-25 14:35

import apps.core.storage.paths
import django.db.models.deletion
import django.utils.timezone
import django_fsm
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("contenttypes", "0002_remove_content_type_name"),
        ("smehub_onboarding", "0001_initial"),
        ("smehub_showcasing", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="FundingApplication",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("application_id", models.CharField(editable=False, max_length=20, unique=True)),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft (auto-saved)"),
                            ("submitted", "Submitted"),
                            ("under_review", "Under review"),
                            ("accepted", "Accepted"),
                            ("rejected", "Rejected"),
                            ("returned_for_info", "Returned for more info"),
                            ("withdrawn", "Withdrawn"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=24,
                        protected=True,
                    ),
                ),
                (
                    "funding_request_amount",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=14, null=True),
                ),
                (
                    "business_plan_file",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_investment,
                    ),
                ),
                (
                    "financial_projections_file",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_investment,
                    ),
                ),
                ("pitch_summary", models.TextField(blank=True)),
                ("use_of_funds", models.TextField(blank=True)),
                ("traction", models.TextField(blank=True)),
                ("team_summary", models.TextField(blank=True)),
                ("auto_saved_payload", models.JSONField(blank=True, default=dict)),
                ("submitted_at", models.DateTimeField(blank=True, null=True)),
                ("decided_at", models.DateTimeField(blank=True, null=True)),
                ("decision_note", models.TextField(blank=True)),
                ("return_message", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "business",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_funding_applications",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "decision_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_funding_applications",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="FundingApplicationDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("label", models.CharField(max_length=255)),
                (
                    "file",
                    models.FileField(upload_to=apps.core.storage.paths.upload_to_smehub_investment),
                ),
                ("uploaded_at", models.DateTimeField(auto_now_add=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="supporting_documents",
                        to="smehub_investment.fundingapplication",
                    ),
                ),
            ],
            options={
                "ordering": ["-uploaded_at"],
            },
        ),
        migrations.CreateModel(
            name="FundingCall",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("summary", models.TextField(blank=True)),
                ("description", models.TextField(blank=True)),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("seed", "Seed grant"),
                            ("poc_grant", "Proof-of-concept grant"),
                            ("growth", "Growth investment"),
                            ("challenge_fund", "Challenge fund"),
                        ],
                        db_index=True,
                        max_length=24,
                    ),
                ),
                (
                    "eligibility",
                    models.JSONField(
                        blank=True, default=dict, help_text="Free-form eligibility payload."
                    ),
                ),
                ("target_sectors", models.JSONField(blank=True, default=list)),
                ("target_stages", models.JSONField(blank=True, default=list)),
                ("target_countries", models.JSONField(blank=True, default=list)),
                (
                    "amount_min",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=14, null=True),
                ),
                (
                    "amount_max",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=14, null=True),
                ),
                ("application_deadline", models.DateTimeField()),
                ("contact_email", models.EmailField(blank=True, max_length=254)),
                (
                    "cover_image",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_investment,
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft"),
                            ("published", "Published"),
                            ("closed", "Closed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=16,
                        protected=True,
                    ),
                ),
                ("published_at", models.DateTimeField(blank=True, null=True)),
                ("closed_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "organiser",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="smehub_funding_calls_organised",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-application_deadline"],
            },
        ),
        migrations.AddField(
            model_name="fundingapplication",
            name="funding_call",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="applications",
                to="smehub_investment.fundingcall",
            ),
        ),
        migrations.CreateModel(
            name="InvestmentReadiness",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("is_ready", models.BooleanField(default=False)),
                (
                    "threshold_breakdown",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Per-criterion booleans (verified_business, baseline_set, incubation_complete, has_partnerships, etc.).",
                    ),
                ),
                (
                    "recommended_actions",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="List of {action, link, reason} dicts shown on the readiness card.",
                    ),
                ),
                ("last_evaluated_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "entrepreneur",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="investment_readiness",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Investment readiness records",
            },
        ),
        migrations.CreateModel(
            name="Investor",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("org_name", models.CharField(max_length=255)),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("angel", "Angel investor"),
                            ("vc", "Venture capital"),
                            ("dfi", "Development finance institution"),
                            ("donor", "Donor / grant maker"),
                            ("foundation", "Foundation"),
                            ("gov_programme", "Government programme"),
                        ],
                        db_index=True,
                        max_length=24,
                    ),
                ),
                (
                    "investment_focus",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="Sector tags (matches BUSINESS choices); FRSME-INV008 facet.",
                    ),
                ),
                (
                    "geographic_preference",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="ISO-2 country codes the investor prefers.",
                    ),
                ),
                (
                    "ticket_size_min",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="Minimum cheque size (USD).",
                        max_digits=14,
                        null=True,
                    ),
                ),
                (
                    "ticket_size_max",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="Maximum cheque size (USD).",
                        max_digits=14,
                        null=True,
                    ),
                ),
                ("contact_email", models.EmailField(blank=True, max_length=254)),
                ("contact_phone", models.CharField(blank=True, max_length=64)),
                ("website", models.URLField(blank=True)),
                ("description", models.TextField(blank=True)),
                (
                    "logo",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_investment,
                    ),
                ),
                (
                    "verification_status",
                    django_fsm.FSMField(
                        choices=[
                            ("pending", "Pending verification"),
                            ("verified", "Verified"),
                            ("rejected", "Rejected"),
                            ("deactivated", "Deactivated"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=16,
                        protected=True,
                    ),
                ),
                ("verified_at", models.DateTimeField(blank=True, null=True)),
                ("rejection_reason", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "contact_user",
                    models.ForeignKey(
                        blank=True,
                        help_text="Account that maintains this investor profile (FRSME-INV002).",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "verified_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["org_name"],
            },
        ),
        migrations.AddField(
            model_name="fundingcall",
            name="investor",
            field=models.ForeignKey(
                blank=True,
                help_text="Optional — investor sponsoring the call.",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="funding_calls",
                to="smehub_investment.investor",
            ),
        ),
        migrations.CreateModel(
            name="DashboardAccessLog",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("viewer_object_id", models.CharField(blank=True, max_length=64)),
                (
                    "scope",
                    models.CharField(
                        choices=[("summary", "Summary"), ("full", "Full")],
                        default="summary",
                        max_length=16,
                    ),
                ),
                (
                    "share_token",
                    models.UUIDField(db_index=True, default=uuid.uuid4, editable=False),
                ),
                ("accessed_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("note", models.CharField(blank=True, max_length=255)),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="dashboard_access_logs",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
                (
                    "viewer_content_type",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="contenttypes.contenttype",
                    ),
                ),
                (
                    "investor",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="dashboard_access_logs",
                        to="smehub_investment.investor",
                    ),
                ),
            ],
            options={
                "ordering": ["-accessed_at"],
            },
        ),
        migrations.CreateModel(
            name="InvestorConnectionRequest",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("message", models.TextField(blank=True)),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("pending", "Pending"),
                            ("accepted", "Accepted"),
                            ("declined", "Declined"),
                            ("withdrawn", "Withdrawn"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=16,
                        protected=True,
                    ),
                ),
                ("decline_reason", models.TextField(blank=True)),
                ("decided_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "business",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="investor_connection_requests",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "deal_room",
                    models.ForeignKey(
                        blank=True,
                        help_text="Set when accepted — FRSME-INV015.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="smehub_showcasing.dealroom",
                    ),
                ),
                (
                    "decided_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="investor_connection_requests",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
                (
                    "investor",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="connection_requests",
                        to="smehub_investment.investor",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="InvestorMatch",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "score",
                    models.DecimalField(decimal_places=4, default=Decimal("0"), max_digits=6),
                ),
                ("reason", models.TextField(blank=True)),
                ("is_fallback", models.BooleanField(default=True)),
                ("generated_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("refreshed_by_signal", models.CharField(blank=True, max_length=64)),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="investor_matches",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
                (
                    "investor",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="entrepreneur_matches",
                        to="smehub_investment.investor",
                    ),
                ),
            ],
            options={
                "ordering": ["-score", "-generated_at"],
            },
        ),
        migrations.CreateModel(
            name="ManualFundingRecord",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("funder_name", models.CharField(max_length=255)),
                ("amount", models.DecimalField(decimal_places=2, max_digits=14)),
                ("currency", models.CharField(default="USD", max_length=8)),
                ("funded_at", models.DateField()),
                ("source_note", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "business",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="manual_funding_records",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "captured_by_admin",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="manual_funding_records",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
                (
                    "investor",
                    models.ForeignKey(
                        blank=True,
                        help_text="Optional link to a directory investor.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="manual_funding_records",
                        to="smehub_investment.investor",
                    ),
                ),
            ],
            options={
                "ordering": ["-funded_at"],
            },
        ),
        migrations.CreateModel(
            name="SMEPerformanceSnapshot",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "payload",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Full aggregation: sp1 baseline, sp2 programmes, sp3 partnerships, sp4 commercialisation, sp5 funding events. FRSME-INV011–012.",
                    ),
                ),
                ("version", models.PositiveSmallIntegerField(default=1)),
                ("refreshed_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("refreshed_by_signal", models.CharField(blank=True, max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "business",
                    models.ForeignKey(
                        blank=True,
                        help_text="Optional — primary business covered by the snapshot.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="performance_snapshots",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "entrepreneur",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="performance_snapshot",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "SME performance snapshots",
            },
        ),
        migrations.AddIndex(
            model_name="fundingapplication",
            index=models.Index(
                fields=["funding_call", "status"], name="smehub_inve_funding_3dd3ed_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="fundingapplication",
            index=models.Index(
                fields=["entrepreneur", "status"], name="smehub_inve_entrepr_331f32_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="fundingapplication",
            constraint=models.UniqueConstraint(
                condition=models.Q(
                    ("status__in", ["draft", "submitted", "under_review", "returned_for_info"])
                ),
                fields=("funding_call", "business"),
                name="uniq_smehub_active_funding_application",
            ),
        ),
        migrations.AddIndex(
            model_name="investor",
            index=models.Index(
                fields=["type", "verification_status"], name="smehub_inve_type_87ee20_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="fundingcall",
            index=models.Index(
                fields=["status", "application_deadline"], name="smehub_inve_status_2b2cf0_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="fundingcall",
            index=models.Index(fields=["type", "status"], name="smehub_inve_type_729e09_idx"),
        ),
        migrations.AddIndex(
            model_name="dashboardaccesslog",
            index=models.Index(
                fields=["entrepreneur", "accessed_at"], name="smehub_inve_entrepr_a460f5_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="investorconnectionrequest",
            index=models.Index(
                fields=["investor", "status"], name="smehub_inve_investo_4bdee5_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="investorconnectionrequest",
            index=models.Index(
                fields=["entrepreneur", "status"], name="smehub_inve_entrepr_70e6d5_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="investorconnectionrequest",
            constraint=models.UniqueConstraint(
                condition=models.Q(("status__in", ["pending", "accepted"])),
                fields=("entrepreneur", "investor", "business"),
                name="uniq_smehub_active_investor_request",
            ),
        ),
        migrations.AddIndex(
            model_name="investormatch",
            index=models.Index(
                fields=["entrepreneur", "score"], name="smehub_inve_entrepr_36d5ec_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="investormatch",
            constraint=models.UniqueConstraint(
                fields=("entrepreneur", "investor"), name="uniq_smehub_investor_match"
            ),
        ),
        migrations.AddIndex(
            model_name="manualfundingrecord",
            index=models.Index(
                fields=["entrepreneur", "funded_at"], name="smehub_inve_entrepr_2fef04_idx"
            ),
        ),
    ]
