# Generated by Django 5.2.6 on 2026-04-08 02:04

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="SyncConfiguration",
            fields=[
                ("config_id", models.AutoField(primary_key=True, serialize=False)),
                (
                    "source_name",
                    models.CharField(
                        help_text="Unique identifier for this data source",
                        max_length=100,
                        unique=True,
                    ),
                ),
                ("base_url", models.URLField(help_text="Base URL of external API")),
                (
                    "api_endpoint",
                    models.CharField(
                        default="/api/organizations/",
                        help_text="REST endpoint relative to base_url",
                        max_length=255,
                    ),
                ),
                (
                    "auth_type",
                    models.CharField(
                        choices=[
                            ("api_key", "API Key"),
                            ("bearer_token", "Bearer Token"),
                            ("basic_auth", "Basic Auth"),
                        ],
                        default="api_key",
                        max_length=50,
                    ),
                ),
                (
                    "auth_key",
                    models.CharField(
                        help_text="API key or token - stored encrypted in production",
                        max_length=500,
                    ),
                ),
                (
                    "timeout_seconds",
                    models.IntegerField(
                        default=30, help_text="Request timeout in seconds"
                    ),
                ),
                (
                    "max_retries",
                    models.IntegerField(
                        default=3,
                        help_text="Maximum retry attempts for failed requests",
                    ),
                ),
                (
                    "retry_delay_seconds",
                    models.IntegerField(
                        default=5, help_text="Initial delay for exponential backoff"
                    ),
                ),
                (
                    "auto_sync_enabled",
                    models.BooleanField(
                        default=False, help_text="Enable automatic scheduled sync"
                    ),
                ),
                (
                    "auto_sync_interval_hours",
                    models.IntegerField(
                        default=24, help_text="Hours between automatic syncs"
                    ),
                ),
                (
                    "batch_size",
                    models.IntegerField(
                        default=100, help_text="Number of records per API request"
                    ),
                ),
                (
                    "cache_enabled",
                    models.BooleanField(default=True, help_text="Cache API responses"),
                ),
                (
                    "cache_ttl_seconds",
                    models.IntegerField(
                        default=3600, help_text="Cache time-to-live in seconds"
                    ),
                ),
                (
                    "webhook_enabled",
                    models.BooleanField(
                        default=False,
                        help_text="Enable webhook support for push updates",
                    ),
                ),
                (
                    "webhook_secret",
                    models.CharField(
                        blank=True,
                        help_text="Secret for webhook signature verification",
                        max_length=500,
                    ),
                ),
                (
                    "field_mapping",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Map external API fields to Organization model fields",
                    ),
                ),
                ("is_active", models.BooleanField(default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("last_tested_at", models.DateTimeField(blank=True, null=True)),
                ("last_test_successful", models.BooleanField(blank=True, null=True)),
            ],
            options={
                "verbose_name": "Sync Configuration",
                "verbose_name_plural": "Sync Configurations",
                "db_table": "sync_configuration",
            },
        ),
        migrations.CreateModel(
            name="Organization",
            fields=[
                (
                    "organization_id",
                    models.AutoField(primary_key=True, serialize=False),
                ),
                ("name", models.CharField(max_length=255, unique=True)),
                ("slug", models.SlugField(max_length=255, unique=True)),
                ("code", models.CharField(max_length=50, unique=True)),
                ("email", models.EmailField(max_length=254, unique=True)),
                ("phone", models.CharField(blank=True, max_length=20)),
                ("website", models.URLField(blank=True, null=True)),
                ("description", models.TextField(blank=True)),
                ("address_line1", models.CharField(blank=True, max_length=255)),
                ("address_line2", models.CharField(blank=True, max_length=255)),
                ("city", models.CharField(blank=True, max_length=100)),
                ("state", models.CharField(blank=True, max_length=100)),
                ("postal_code", models.CharField(blank=True, max_length=20)),
                ("country", models.CharField(blank=True, max_length=100)),
                (
                    "external_id",
                    models.CharField(
                        db_index=True,
                        help_text="Unique identifier from external API source",
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "external_source",
                    models.CharField(
                        default="default_source",
                        help_text="Name of external API source",
                        max_length=100,
                    ),
                ),
                (
                    "external_metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Store additional metadata from external API",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("active", "Active"),
                            ("inactive", "Inactive"),
                            ("suspended", "Suspended"),
                            ("archived", "Archived"),
                        ],
                        default="active",
                        max_length=20,
                    ),
                ),
                ("is_active", models.BooleanField(default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "last_synced",
                    models.DateTimeField(
                        blank=True,
                        help_text="Timestamp of last successful sync",
                        null=True,
                    ),
                ),
            ],
            options={
                "verbose_name": "Organization",
                "verbose_name_plural": "Organizations",
                "db_table": "organization",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["external_id", "external_source"],
                        name="organizatio_externa_c8e70f_idx",
                    ),
                    models.Index(fields=["code"], name="organizatio_code_9ea172_idx"),
                    models.Index(
                        fields=["status"], name="organizatio_status_3d172d_idx"
                    ),
                    models.Index(
                        fields=["last_synced"], name="organizatio_last_sy_5593cd_idx"
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="SyncLog",
            fields=[
                ("sync_id", models.AutoField(primary_key=True, serialize=False)),
                ("external_source", models.CharField(max_length=100)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("started", "Started"),
                            ("in_progress", "In Progress"),
                            ("completed", "Completed"),
                            ("failed", "Failed"),
                            ("partial", "Partial Success"),
                        ],
                        default="started",
                        max_length=20,
                    ),
                ),
                ("started_at", models.DateTimeField(auto_now_add=True)),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                ("duration_seconds", models.FloatField(blank=True, null=True)),
                ("total_fetched", models.IntegerField(default=0)),
                ("created_count", models.IntegerField(default=0)),
                ("updated_count", models.IntegerField(default=0)),
                ("skipped_count", models.IntegerField(default=0)),
                ("error_count", models.IntegerField(default=0)),
                ("error_message", models.TextField(blank=True)),
                ("error_traceback", models.TextField(blank=True)),
                ("failed_records", models.JSONField(blank=True, default=list)),
                (
                    "triggered_by",
                    models.CharField(
                        blank=True,
                        help_text="User or system that triggered the sync",
                        max_length=100,
                    ),
                ),
                (
                    "sync_type",
                    models.CharField(
                        choices=[("full", "Full Sync"), ("partial", "Partial Sync")],
                        default="full",
                        max_length=50,
                    ),
                ),
                ("notes", models.TextField(blank=True)),
            ],
            options={
                "verbose_name": "Sync Log",
                "verbose_name_plural": "Sync Logs",
                "db_table": "sync_log",
                "ordering": ["-started_at"],
                "indexes": [
                    models.Index(
                        fields=["external_source", "status"],
                        name="sync_log_externa_1be1e9_idx",
                    ),
                    models.Index(
                        fields=["started_at"], name="sync_log_started_515ff2_idx"
                    ),
                ],
            },
        ),
    ]
