"""
inventory/migrations/0011_seed_internal_bookstore.py
Create the single internal "Bookstore" row representing Tai Bookstore /
website itself, so walk-in and website sales can resolve a BookstoreListing
with the fixed-agreed-price payout mechanics instead of the plain 2-way
split. See inventory/services/sales_service.py:_resolve_internal_bookstore()
and inventory/services/inventory_service.py:_ensure_internal_listing().
"""

from decimal import Decimal

from django.db import migrations


def seed_internal_bookstore(apps, schema_editor):
    Bookstore = apps.get_model("inventory", "Bookstore")
    StockLocation = apps.get_model("inventory", "StockLocation")

    if Bookstore.objects.filter(is_internal=True).exists():
        return

    main_wh = StockLocation.objects.filter(code="MAIN-WH").first()
    Bookstore.objects.create(
        name="Tai Bookstore & Website",
        stock_location=main_wh,
        default_bookstore_cut_percent=Decimal("0.00"),
        is_internal=True,
        is_active=True,
    )


def unseed_internal_bookstore(apps, schema_editor):
    Bookstore = apps.get_model("inventory", "Bookstore")
    Bookstore.objects.filter(is_internal=True, name="Tai Bookstore & Website").delete()


class Migration(migrations.Migration):
    dependencies = [
        ("inventory", "0010_bookstore_is_internal"),
        ("inventory", "0002_seed_defaults"),
    ]

    operations = [
        migrations.RunPython(seed_internal_bookstore, unseed_internal_bookstore),
    ]
