"""Section handler registry + hull handler."""

from unittest.mock import patch

from django.test import TestCase

from shop.models import HULL, HullListing, Shop, ShopSource
from shop.sections import handler_for
from shop.tests._corptools import (
    make_asset,
    make_owner_character,
    make_ship_sde_type,
    make_ship_type,
)


class RegistryTests(TestCase):
    def test_hull_handler_registered(self):
        h = handler_for("hulls")
        self.assertIsNotNone(h)
        self.assertEqual(h.section, "hulls")

    def test_fitted_handler_registered(self):
        h = handler_for("fitted")
        self.assertIsNotNone(h)
        self.assertEqual(h.section, "fitted")


class HullHandlerStockroomAddedTests(TestCase):
    def setUp(self):
        self.user, self.char = make_owner_character()
        self.station = 60003760
        make_ship_sde_type(type_id=671, category_id=6)
        self.ship = make_ship_type()
        self.shop = Shop.objects.create(slug="s", name="S", created_by=self.user)

    def _asset(self, item_id, qty=1):
        return make_asset(
            self.char,
            type_id=self.ship.id,
            item_id=item_id,
            location_id=self.station,
            quantity=qty,
        )

    def test_added_autolists_and_recomputes(self):
        self._asset(1)
        self._asset(2)
        source = ShopSource.objects.create(
            shop=self.shop,
            section=HULL,
            character_audit=self.char,
            location_id=self.station,
        )
        handler_for("hulls").stockroom_added(
            source, {"price_mode": "fixed", "price_pct": None}
        )
        listing = HullListing.objects.get(shop=self.shop, eve_type=self.ship)
        self.assertEqual(listing.stock, 2)
        self.assertEqual(source.price_mode, "fixed")


class BpcHandlerTests(TestCase):
    @patch("shop.sections.bpc.blueprints.refresh_character_blueprints")
    def test_registered_and_recomputes_on_add(self, _mock_refresh):
        # mock the ESI mirror fetch (no token in tests) so it doesn't prune the
        # blueprint row we seed below; the test exercises the recompute path.
        from shop.models import BPC, BpcListing, CharacterBlueprint, Shop, ShopSource
        from shop.sections import handler_for
        from shop.tests._corptools import (
            make_asset,
            make_owner_character,
            make_ship_type,
        )

        user, char = make_owner_character()
        station = 60003760
        bp_type = make_ship_type(type_id=12040, name="Rifter Blueprint")
        shop = Shop.objects.create(slug="s", name="S", created_by=user)
        source = ShopSource.objects.create(
            shop=shop, section=BPC, character_audit=char, location_id=station
        )
        make_asset(
            char,
            type_id=bp_type.id,
            item_id=1,
            location_id=station,
            quantity=1,
            blueprint_copy=True,
        )
        CharacterBlueprint.objects.create(
            character=char, item_id=1, type_id=12040, me=10, te=20, runs=50, quantity=-2
        )
        handler = handler_for(BPC)
        self.assertIsNotNone(handler)
        handler.stockroom_added(source, {})
        self.assertEqual(
            BpcListing.objects.get(shop=shop, eve_type=bp_type).variants.count(), 1
        )
