# Generated by Django 5.2.13 on 2026-04-25 08:27

import apps.core.storage.paths
import django.db.models.deletion
import django_fsm
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name="BuyerRegistryEntry",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("organisation_name", models.CharField(max_length=255)),
                (
                    "org_type",
                    models.CharField(
                        choices=[
                            ("wholesale", "Wholesaler / aggregator"),
                            ("retail", "Retailer"),
                            ("exporter", "Exporter"),
                            ("processor", "Processor"),
                            ("institutional", "Institutional buyer"),
                            ("government", "Government / parastatal"),
                            ("other", "Other"),
                        ],
                        db_index=True,
                        max_length=24,
                    ),
                ),
                ("sectors_sourced", models.JSONField(blank=True, default=list)),
                ("geographic_coverage", models.JSONField(blank=True, default=list)),
                ("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)),
                (
                    "typical_volume",
                    models.CharField(
                        blank=True,
                        help_text="Free-form: e.g. '5–10 tonnes/month maize'.",
                        max_length=255,
                    ),
                ),
                (
                    "verification_status",
                    django_fsm.FSMField(
                        choices=[
                            ("pending", "Pending verification"),
                            ("verified", "Verified"),
                            ("rejected", "Rejected"),
                        ],
                        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)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="smehub_buyer_registry",
                        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={
                "verbose_name_plural": "Buyer registry entries",
                "ordering": ["organisation_name"],
            },
        ),
        migrations.CreateModel(
            name="MarketDemandListing",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("listing_id", models.CharField(editable=False, max_length=20, unique=True)),
                (
                    "kind",
                    models.CharField(
                        choices=[
                            ("problem", "Problem statement"),
                            ("tender", "Tender"),
                            ("procurement", "Procurement need"),
                            ("offtake", "Off-take opportunity"),
                        ],
                        default="problem",
                        max_length=16,
                    ),
                ),
                ("description", models.TextField()),
                ("sector_targeting", models.JSONField(blank=True, default=list)),
                ("geographic_targeting", models.JSONField(blank=True, default=list)),
                (
                    "quantity",
                    models.CharField(
                        blank=True,
                        help_text="Free-form quantity / volume requirement.",
                        max_length=255,
                    ),
                ),
                ("deadline", models.DateTimeField(blank=True, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[("open", "Open"), ("closed", "Closed"), ("awarded", "Awarded")],
                        db_index=True,
                        default="open",
                        max_length=16,
                    ),
                ),
                ("closed_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "buyer",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="demand_listings",
                        to="smehub_marketplace.buyerregistryentry",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="ProductListing",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "listing_id",
                    models.CharField(
                        editable=False,
                        help_text="Auto-generated public identifier.",
                        max_length=20,
                        unique=True,
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True)),
                ("sector", models.CharField(db_index=True, max_length=128)),
                ("sub_sector", models.CharField(blank=True, max_length=128)),
                (
                    "pricing",
                    models.CharField(
                        blank=True,
                        help_text="Free-form pricing string (e.g. 'USD 12.5 / kg', 'POA').",
                        max_length=255,
                    ),
                ),
                ("minimum_order", models.CharField(blank=True, max_length=128)),
                (
                    "availability",
                    models.CharField(
                        choices=[
                            ("in_stock", "In stock"),
                            ("to_order", "Available to order"),
                            ("seasonal", "Seasonal"),
                            ("out_of_stock", "Out of stock"),
                        ],
                        default="in_stock",
                        max_length=24,
                    ),
                ),
                (
                    "geographic_reach",
                    models.JSONField(
                        blank=True, default=list, help_text="ISO-2 codes the listing serves."
                    ),
                ),
                (
                    "cover_image",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_marketplace,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("draft", "Draft"),
                            ("published", "Published"),
                            ("unpublished", "Unpublished"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=16,
                    ),
                ),
                ("published_at", models.DateTimeField(blank=True, null=True)),
                ("unpublished_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.CASCADE,
                        related_name="product_listings",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="product_listings",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="DemandResponse",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("message", models.TextField()),
                ("pricing_offer", models.CharField(blank=True, max_length=255)),
                (
                    "attachment",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_marketplace,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("submitted", "Submitted"),
                            ("shortlisted", "Shortlisted"),
                            ("awarded", "Awarded"),
                            ("declined", "Declined"),
                            ("withdrawn", "Withdrawn"),
                        ],
                        db_index=True,
                        default="submitted",
                        max_length=16,
                    ),
                ),
                ("decided_at", models.DateTimeField(blank=True, null=True)),
                ("decision_note", 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.CASCADE,
                        related_name="demand_responses",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "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="demand_responses",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
                (
                    "demand_listing",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="responses",
                        to="smehub_marketplace.marketdemandlisting",
                    ),
                ),
                (
                    "product_listing",
                    models.ForeignKey(
                        blank=True,
                        help_text="Optional reference to a published product listing.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="demand_responses",
                        to="smehub_marketplace.productlisting",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="ProductMedia",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "kind",
                    models.CharField(
                        choices=[
                            ("image", "Image"),
                            ("document", "Document"),
                            ("video_link", "Video link"),
                        ],
                        default="image",
                        max_length=16,
                    ),
                ),
                ("title", models.CharField(blank=True, max_length=255)),
                (
                    "file",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_marketplace,
                    ),
                ),
                ("external_url", models.URLField(blank=True)),
                ("sort_order", models.PositiveSmallIntegerField(default=0)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "listing",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="media",
                        to="smehub_marketplace.productlisting",
                    ),
                ),
            ],
            options={
                "ordering": ["sort_order", "id"],
            },
        ),
        migrations.AddIndex(
            model_name="marketdemandlisting",
            index=models.Index(fields=["status", "deadline"], name="smehub_mark_status_26c405_idx"),
        ),
        migrations.AddIndex(
            model_name="productlisting",
            index=models.Index(fields=["sector", "status"], name="smehub_mark_sector_933796_idx"),
        ),
        migrations.AddIndex(
            model_name="productlisting",
            index=models.Index(
                fields=["entrepreneur", "status"], name="smehub_mark_entrepr_c9fde4_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="demandresponse",
            index=models.Index(
                fields=["demand_listing", "status"], name="smehub_mark_demand__faa413_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="demandresponse",
            index=models.Index(
                fields=["entrepreneur", "status"], name="smehub_mark_entrepr_e656bb_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="demandresponse",
            constraint=models.UniqueConstraint(
                condition=models.Q(("status__in", ["submitted", "shortlisted"])),
                fields=("demand_listing", "business"),
                name="uniq_smehub_active_demand_response",
            ),
        ),
    ]
