from django.db import IntegrityError
from django.test import TestCase

from shop.models import FittedListing, Shop, ShopSource
from shop.models.base import FITTED
from shop.tests._corptools import make_owner_character, make_ship_type


class FittedListingModelTests(TestCase):
    def setUp(self):
        self.user, self.char = make_owner_character()
        self.shop = Shop.objects.create(slug="s", name="S", created_by=self.user)
        self.hull = make_ship_type(type_id=23773, name="Ragnarok")

    def test_create_and_defaults(self):
        listing = FittedListing.objects.create(
            shop=self.shop,
            eve_type=self.hull,
            item_id=9001,
            ship_name="Doomsday Express",
            fit_json={"slots": []},
        )
        self.assertEqual(listing.price, 0)  # Ask by default
        self.assertFalse(listing.featured)  # from FeaturedListing
        self.assertEqual(listing.price_display, "Ask")
        self.assertEqual(listing.stock, 1)  # present by default
        self.assertFalse(listing.persist)  # auto-remove when gone by default

    def test_description_html_renders_markdown(self):
        listing = FittedListing.objects.create(
            shop=self.shop,
            eve_type=self.hull,
            item_id=9009,
            ship_name="x",
            fit_json={},
            description="Shiny **PvP** rag",
        )
        self.assertIn("<strong>PvP</strong>", listing.description_html)

    def test_price_display_formats_isk(self):
        listing = FittedListing.objects.create(
            shop=self.shop,
            eve_type=self.hull,
            item_id=9002,
            ship_name="x",
            fit_json={},
            price=90_000_000_000,
        )
        self.assertEqual(listing.price_display, "90B ISK")

    def test_unique_per_shop_item(self):
        FittedListing.objects.create(
            shop=self.shop, eve_type=self.hull, item_id=9003, ship_name="a", fit_json={}
        )
        with self.assertRaises(IntegrityError):
            FittedListing.objects.create(
                shop=self.shop,
                eve_type=self.hull,
                item_id=9003,
                ship_name="b",
                fit_json={},
            )


class ShopSourceFittedFieldsTests(TestCase):
    def test_name_filter_and_strip_marker_fields(self):
        user, char = make_owner_character()
        shop = Shop.objects.create(slug="s2", name="S2", created_by=user)
        src = ShopSource.objects.create(
            shop=shop,
            section=FITTED,
            character_audit=char,
            location_id=60003760,
            name_filter="[SALE]",
            strip_marker=True,
        )
        src.refresh_from_db()
        self.assertEqual(src.name_filter, "[SALE]")
        self.assertTrue(src.strip_marker)

    def test_name_filter_defaults_blank_strip_true(self):
        user, char = make_owner_character()
        shop = Shop.objects.create(slug="s3", name="S3", created_by=user)
        src = ShopSource.objects.create(
            shop=shop,
            section=FITTED,
            character_audit=char,
            location_id=60003760,
        )
        self.assertEqual(src.name_filter, "")
        self.assertTrue(src.strip_marker)
