"""Initial payment gateway models."""

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name="PaymentAddress",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("blockchain", models.CharField(db_index=True, max_length=50)),
                ("asset", models.CharField(default="USDT", max_length=20)),
                ("address", models.CharField(max_length=255, unique=True)),
                ("status", models.CharField(choices=[("active", "Active"), ("revoked", "Revoked"), ("expired", "Expired")], default="active", max_length=20)),
                ("label", models.CharField(blank=True, help_text="Optional label or derivation path reference from the gateway.", max_length=100)),
                ("expires_at", models.DateTimeField(blank=True, null=True)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("user", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="payment_addresses", to=settings.AUTH_USER_MODEL)),
            ],
            options={
                "db_table": "payment_addresses",
            },
        ),
        migrations.CreateModel(
            name="Deposit",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("blockchain", models.CharField(db_index=True, max_length=50)),
                ("asset", models.CharField(default="USDT", max_length=20)),
                ("amount", models.DecimalField(decimal_places=8, max_digits=20)),
                ("transaction_hash", models.CharField(db_index=True, max_length=255)),
                ("confirmations", models.PositiveIntegerField(default=0)),
                ("status", models.CharField(choices=[("pending", "Pending"), ("confirmed", "Confirmed"), ("failed", "Failed"), ("duplicate", "Duplicate")], default="pending", max_length=20)),
                ("detected_at", models.DateTimeField(auto_now_add=True)),
                ("confirmed_at", models.DateTimeField(blank=True, null=True)),
                ("raw_payload", models.JSONField(blank=True, default=dict)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("address", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="deposits", to="payments.paymentaddress")),
                ("transaction", models.OneToOneField(blank=True, help_text="Corresponding wallet transaction once balance is credited.", null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="deposit", to="transactions.transaction")),
                ("user", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="deposits", to=settings.AUTH_USER_MODEL)),
            ],
            options={
                "db_table": "payment_deposits",
            },
        ),
        migrations.CreateModel(
            name="WithdrawalRequest",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("blockchain", models.CharField(db_index=True, max_length=50)),
                ("asset", models.CharField(default="USDT", max_length=20)),
                ("amount", models.DecimalField(decimal_places=8, max_digits=20)),
                ("destination_address", models.CharField(max_length=255)),
                ("status", models.CharField(choices=[("requested", "Requested"), ("processing", "Processing"), ("sent", "Sent"), ("failed", "Failed"), ("cancelled", "Cancelled")], default="requested", max_length=20)),
                ("gateway_reference", models.CharField(blank=True, db_index=True, help_text="External reference or request id from the payment gateway.", max_length=255)),
                ("transaction_hash", models.CharField(blank=True, db_index=True, help_text="On-chain hash when available.", max_length=255)),
                ("requested_at", models.DateTimeField(auto_now_add=True)),
                ("processed_at", models.DateTimeField(blank=True, null=True)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("transaction", models.OneToOneField(blank=True, help_text="Wallet transaction when the withdrawal is debited.", null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="withdrawal_request", to="transactions.transaction")),
                ("user", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="withdrawal_requests", to=settings.AUTH_USER_MODEL)),
            ],
            options={
                "db_table": "payment_withdrawals",
            },
        ),
        migrations.CreateModel(
            name="GatewayCallbackLog",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("event_type", models.CharField(db_index=True, max_length=100)),
                ("status", models.CharField(choices=[("pending", "Pending"), ("processed", "Processed"), ("error", "Error")], default="pending", max_length=20)),
                ("error_message", models.TextField(blank=True)),
                ("payload", models.JSONField(blank=True, default=dict)),
                ("headers", models.JSONField(blank=True, default=dict)),
                ("received_at", models.DateTimeField(auto_now_add=True)),
                ("processed_at", models.DateTimeField(blank=True, null=True)),
                ("related_deposit", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="callback_logs", to="payments.deposit")),
                ("related_withdrawal", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="callback_logs", to="payments.withdrawalrequest")),
            ],
            options={
                "db_table": "payment_callback_logs",
            },
        ),
        migrations.AddIndex(
            model_name="paymentaddress",
            index=models.Index(fields=["user", "blockchain"], name="payment_add_user_id_1a2826_idx"),
        ),
        migrations.AddIndex(
            model_name="paymentaddress",
            index=models.Index(fields=["status", "expires_at"], name="payment_add_status_2b069a_idx"),
        ),
        migrations.AddIndex(
            model_name="deposit",
            index=models.Index(fields=["user", "status"], name="payment_dep_user_id_0b0e57_idx"),
        ),
        migrations.AddIndex(
            model_name="deposit",
            index=models.Index(fields=["transaction_hash", "blockchain"], name="payment_dep_transac_233b5e_idx"),
        ),
        migrations.AddConstraint(
            model_name="deposit",
            constraint=models.UniqueConstraint(fields=("transaction_hash", "blockchain"), name="unique_payment_deposit_tx"),
        ),
        migrations.AddIndex(
            model_name="withdrawalrequest",
            index=models.Index(fields=["user", "status"], name="payment_wit_user_id_59b01b_idx"),
        ),
        migrations.AddIndex(
            model_name="withdrawalrequest",
            index=models.Index(fields=["gateway_reference"], name="payment_wit_gateway_9a9c62_idx"),
        ),
        migrations.AddIndex(
            model_name="withdrawalrequest",
            index=models.Index(fields=["transaction_hash", "blockchain"], name="payment_wit_transac_1da6c9_idx"),
        ),
        migrations.AddIndex(
            model_name="gatewaycallbacklog",
            index=models.Index(fields=["event_type", "status"], name="payment_cal_event_t_2c0f48_idx"),
        ),
        migrations.AddIndex(
            model_name="gatewaycallbacklog",
            index=models.Index(fields=["received_at"], name="payment_cal_received_4df0a6_idx"),
        ),
    ]
