import decimal
import uuid
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
    dependencies = [
        ("club_finance", "0004_contract_termination_compensation"),
        ("club_assets", "0001_initial"),
        ("contracts", "0006_compensation_clauses_payroll_lifecycle"),
        ("players", "0003_player_form_morale_match_experience_engine"),
        ("economy", "0012_contract_termination_compensation"),
    ]

    operations = [
        migrations.CreateModel(
            name="GLAccount",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("code", models.CharField(max_length=24)),
                ("name", models.CharField(max_length=140)),
                ("account_type", models.CharField(choices=[("ASSET", "Asset"), ("LIABILITY", "Liability"), ("EQUITY", "Equity"), ("REVENUE", "Revenue"), ("EXPENSE", "Expense")], max_length=12)),
                ("normal_balance", models.CharField(choices=[("DEBIT", "Debit"), ("CREDIT", "Credit")], max_length=8)),
                ("is_system", models.BooleanField(default=True)),
                ("is_control_account", models.BooleanField(default=False)),
                ("allow_manual_entries", models.BooleanField(default=False)),
                ("is_active", models.BooleanField(default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="gl_accounts", to="clubs.club")),
                ("parent", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="children", to="club_finance.glaccount")),
            ],
            options={"ordering": ["code", "id"], "indexes": [models.Index(fields=["club", "account_type", "is_active"], name="cf_gl_acct_type_idx")]},
        ),
        migrations.AddConstraint(model_name="glaccount", constraint=models.UniqueConstraint(fields=("club", "code"), name="cf_gl_account_club_code_uq")),
        migrations.CreateModel(
            name="JournalEntry",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("accounting_date", models.DateField()),
                ("journal_no", models.CharField(max_length=80, unique=True)),
                ("status", models.CharField(choices=[("POSTED", "Posted"), ("REVERSED", "Reversed")], default="POSTED", max_length=12)),
                ("source_type", models.CharField(max_length=64)),
                ("source_id", models.CharField(blank=True, default="", max_length=120)),
                ("memo", models.CharField(blank=True, default="", max_length=255)),
                ("idempotency_key", models.CharField(max_length=180, unique=True)),
                ("checksum", models.CharField(max_length=64)),
                ("posted_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="journal_entries", to="clubs.club")),
                ("economy_ledger", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="club_gl_journals", to="economy.ledgerentry")),
                ("period", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="journal_entries", to="club_finance.accountingperiod")),
                ("reversal_of", models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="reversal_entry", to="club_finance.journalentry")),
            ],
            options={"ordering": ["accounting_date", "posted_at", "id"], "indexes": [models.Index(fields=["club", "accounting_date"], name="cf_gl_journal_date_idx"), models.Index(fields=["club", "source_type", "source_id"], name="cf_gl_journal_source_idx")]},
        ),
        migrations.AddConstraint(model_name="journalentry", constraint=models.UniqueConstraint(condition=models.Q(("economy_ledger__isnull", False)), fields=("club", "economy_ledger"), name="cf_gl_one_journal_per_club_ledger")),
        migrations.AddConstraint(model_name="journalentry", constraint=models.CheckConstraint(condition=~models.Q(("checksum", "")), name="cf_gl_journal_checksum_nonempty")),
        migrations.CreateModel(
            name="JournalLine",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("line_no", models.PositiveSmallIntegerField()),
                ("debit", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("credit", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("description", models.CharField(blank=True, default="", max_length=255)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("account", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="journal_lines", to="club_finance.glaccount")),
                ("entry", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="lines", to="club_finance.journalentry")),
            ],
            options={"ordering": ["entry_id", "line_no"], "indexes": [models.Index(fields=["account", "entry"], name="cf_gl_line_account_idx")]},
        ),
        migrations.AddConstraint(model_name="journalline", constraint=models.UniqueConstraint(fields=("entry", "line_no"), name="cf_gl_line_no_uq")),
        migrations.AddConstraint(model_name="journalline", constraint=models.CheckConstraint(condition=models.Q(("debit__gte", 0), ("credit__gte", 0)), name="cf_gl_line_nonnegative")),
        migrations.AddConstraint(model_name="journalline", constraint=models.CheckConstraint(condition=models.Q(models.Q(("debit__gt", 0), ("credit", 0)), models.Q(("credit__gt", 0), ("debit", 0)), _connector="OR"), name="cf_gl_line_one_side")),
        migrations.CreateModel(
            name="AccrualDocument",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("document_type", models.CharField(choices=[("PAYABLE", "Accounts payable"), ("RECEIVABLE", "Accounts receivable")], max_length=12)),
                ("counterparty_label", models.CharField(blank=True, default="", max_length=160)),
                ("original_amount", models.DecimalField(decimal_places=8, max_digits=24)),
                ("settled_amount", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("recognized_on", models.DateField()),
                ("due_date", models.DateField()),
                ("status", models.CharField(choices=[("OPEN", "Open"), ("PARTIAL", "Partially settled"), ("SETTLED", "Settled"), ("VOID", "Void")], default="OPEN", max_length=12)),
                ("source_type", models.CharField(max_length=64)),
                ("source_id", models.CharField(max_length=120)),
                ("idempotency_key", models.CharField(max_length=180, unique=True)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="accrual_documents", to="clubs.club")),
                ("control_account", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="controlled_accrual_documents", to="club_finance.glaccount")),
                ("counterparty_wallet", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="club_finance_accrual_documents", to="economy.wallet")),
                ("period", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="accrual_documents", to="club_finance.accountingperiod")),
                ("recognition_account", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="recognized_accrual_documents", to="club_finance.glaccount")),
                ("recognition_journal", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="recognized_accrual_document", to="club_finance.journalentry")),
            ],
            options={"ordering": ["due_date", "created_at", "id"], "indexes": [models.Index(fields=["club", "document_type", "status", "due_date"], name="cf_accrual_due_idx")]},
        ),
        migrations.AddConstraint(model_name="accrualdocument", constraint=models.CheckConstraint(condition=models.Q(("original_amount__gt", 0)), name="cf_accrual_amount_positive")),
        migrations.AddConstraint(model_name="accrualdocument", constraint=models.CheckConstraint(condition=models.Q(("settled_amount__gte", 0)), name="cf_accrual_settled_nonneg")),
        migrations.AddConstraint(model_name="accrualdocument", constraint=models.CheckConstraint(condition=models.Q(("settled_amount__lte", models.F("original_amount"))), name="cf_accrual_settled_lte")),
        migrations.AddConstraint(model_name="accrualdocument", constraint=models.UniqueConstraint(fields=("club", "document_type", "source_type", "source_id"), name="cf_accrual_source_uq")),
        migrations.CreateModel(
            name="AccrualSettlement",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("amount", models.DecimalField(decimal_places=8, max_digits=24)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("document", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="settlements", to="club_finance.accrualdocument")),
                ("journal_entry", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="accrual_settlements", to="club_finance.journalentry")),
                ("ledger_entry", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="accrual_settlements", to="economy.ledgerentry")),
            ],
        ),
        migrations.AddConstraint(model_name="accrualsettlement", constraint=models.CheckConstraint(condition=models.Q(("amount__gt", 0)), name="cf_accrual_settlement_positive")),
        migrations.AddConstraint(model_name="accrualsettlement", constraint=models.UniqueConstraint(fields=("document", "ledger_entry"), name="cf_accrual_settlement_doc_ledger_uq")),
        migrations.CreateModel(
            name="FixedAssetBook",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("acquisition_cost", models.DecimalField(decimal_places=8, max_digits=24)),
                ("residual_value", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("accumulated_depreciation", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("useful_life_months", models.PositiveIntegerField()),
                ("depreciation_method", models.CharField(choices=[("STRAIGHT_LINE", "Straight line")], default="STRAIGHT_LINE", max_length=20)),
                ("placed_in_service", models.DateField()),
                ("last_depreciated_through", models.DateField(blank=True, null=True)),
                ("disposed_on", models.DateField(blank=True, null=True)),
                ("source_type", models.CharField(blank=True, default="", max_length=64)),
                ("source_id", models.CharField(blank=True, default="", max_length=120)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("asset", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="accounting_books", to="club_assets.clubasset")),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="fixed_asset_books", to="clubs.club")),
            ],
            options={"indexes": [models.Index(fields=["club", "disposed_on"], name="cf_fixed_active_idx")]},
        ),
        migrations.AddConstraint(model_name="fixedassetbook", constraint=models.CheckConstraint(condition=models.Q(("acquisition_cost__gt", 0)), name="cf_fixed_cost_positive")),
        migrations.AddConstraint(model_name="fixedassetbook", constraint=models.CheckConstraint(condition=models.Q(("residual_value__gte", 0)), name="cf_fixed_residual_nonneg")),
        migrations.AddConstraint(model_name="fixedassetbook", constraint=models.CheckConstraint(condition=models.Q(("accumulated_depreciation__gte", 0)), name="cf_fixed_accum_depr_nonneg")),
        migrations.AddConstraint(model_name="fixedassetbook", constraint=models.CheckConstraint(condition=models.Q(("useful_life_months__gte", 1)), name="cf_fixed_life_positive")),
        migrations.AddConstraint(model_name="fixedassetbook", constraint=models.UniqueConstraint(condition=models.Q(("disposed_on__isnull", True)), fields=("asset", "club"), name="cf_fixed_active_asset_club_uq")),
        migrations.CreateModel(
            name="PlayerRegistrationAsset",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("acquisition_cost", models.DecimalField(decimal_places=8, max_digits=24)),
                ("accumulated_amortization", models.DecimalField(decimal_places=8, default=decimal.Decimal("0"), max_digits=24)),
                ("starts_on", models.DateField()),
                ("ends_on", models.DateField()),
                ("last_amortized_through", models.DateField(blank=True, null=True)),
                ("disposed_on", models.DateField(blank=True, null=True)),
                ("source_transfer_id", models.UUIDField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_registration_assets", to="clubs.club")),
                ("contract", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="registration_asset", to="contracts.contract")),
                ("player", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="registration_assets", to="players.player")),
            ],
            options={"indexes": [models.Index(fields=["club", "disposed_on"], name="cf_reg_asset_active_idx")]},
        ),
        migrations.AddConstraint(model_name="playerregistrationasset", constraint=models.CheckConstraint(condition=models.Q(("acquisition_cost__gt", 0)), name="cf_reg_asset_cost_positive")),
        migrations.AddConstraint(model_name="playerregistrationasset", constraint=models.CheckConstraint(condition=models.Q(("accumulated_amortization__gte", 0)), name="cf_reg_asset_amort_nonneg")),
        migrations.AddConstraint(model_name="playerregistrationasset", constraint=models.CheckConstraint(condition=models.Q(("ends_on__gt", models.F("starts_on"))), name="cf_reg_asset_dates_order")),
        migrations.CreateModel(
            name="FinancialStatementSnapshot",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("statement_type", models.CharField(choices=[("INCOME_STATEMENT", "Income statement"), ("BALANCE_SHEET", "Balance sheet"), ("CASH_FLOW", "Cash flow statement"), ("TRIAL_BALANCE", "Trial balance")], max_length=24)),
                ("as_of", models.DateField()),
                ("payload", models.JSONField(default=dict)),
                ("checksum", models.CharField(max_length=64)),
                ("generated_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="financial_statement_snapshots", to="clubs.club")),
                ("period", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="statement_snapshots", to="club_finance.accountingperiod")),
            ],
            options={"ordering": ["period_id", "statement_type", "generated_at"]},
        ),
        migrations.AddConstraint(model_name="financialstatementsnapshot", constraint=models.UniqueConstraint(fields=("period", "statement_type"), name="cf_statement_period_type_uq")),
        migrations.AddConstraint(model_name="financialstatementsnapshot", constraint=models.CheckConstraint(condition=~models.Q(("checksum", "")), name="cf_statement_checksum_nonempty")),
    ]
