from ttfrog.db.base import StatsEnum
from ttfrog.db.schema.constants import DamageType
from ttfrog.db.schema.prototypes import Rarity
from ttfrog.five_e_tools import importer


def test_imports(db):
    with db.transaction():
        db.add_or_update(list(importer.get_all()))

        # buff
        heroes_feast = db.BaseSpell.filter_by(name="Heroes' Feast", source="PHB").one()
        assert heroes_feast.school == "Conjuration"
        assert heroes_feast.target_range == 30
        assert heroes_feast.target_shape == "point"
        assert heroes_feast.target_size is None
        assert heroes_feast.time == "10 minute"
        assert heroes_feast.duration == "instant"
        assert heroes_feast.level == 6
        assert heroes_feast.concentration is False
        assert heroes_feast.components.startswith("verbal, somatic, materials")
        assert heroes_feast.damage_type is None
        assert heroes_feast.damage_die is None
        assert heroes_feast.saving_throw is None
        assert "a gem-encrusted bowl worth at least 1,000 gp" in heroes_feast.components

        # area of effect
        meteor_swarm = db.BaseSpell.filter_by(name="Meteor Swarm", source="PHB").one()
        assert meteor_swarm.school == "Evocation"
        assert meteor_swarm.target_range == 5280
        assert meteor_swarm.target_shape == "point"
        assert meteor_swarm.target_size is None
        assert meteor_swarm.time == "1 action"
        assert meteor_swarm.duration == "instant"
        assert meteor_swarm.level == 9
        assert meteor_swarm.components == "verbal, somatic"
        assert meteor_swarm.concentration is False
        assert meteor_swarm.damage_die == "20d6"
        assert meteor_swarm.damage_type == "fire"
        assert meteor_swarm.saving_throw == StatsEnum.dexterity
        assert meteor_swarm.description.startswith("Blazing orbs of fire")

        # spell attack, saving throw
        acid_splash = db.BaseSpell.filter_by(name="Acid Splash", source="PHB").one()
        assert acid_splash.school == "Conjuration"
        assert acid_splash.target_range == 60
        assert acid_splash.target_shape == "point"
        assert acid_splash.target_size is None
        assert acid_splash.time == "1 action"
        assert acid_splash.duration == "instant"
        assert acid_splash.level == 0
        assert acid_splash.concentration is False
        assert acid_splash.components == "verbal, somatic"
        assert acid_splash.damage_die == "1d6"
        assert acid_splash.damage_type == "acid"
        assert acid_splash.saving_throw == StatsEnum.dexterity
        assert "You hurl a bubble of acid" in acid_splash.description

        # spell attack, ranged
        eldritch_blast = db.BaseSpell.filter_by(name="Eldritch Blast", source="PHB").one()
        assert eldritch_blast.school == "Evocation"
        assert eldritch_blast.target_range == 120
        assert eldritch_blast.target_shape == "point"
        assert eldritch_blast.target_size is None
        assert eldritch_blast.time == "1 action"
        assert eldritch_blast.duration == "instant"
        assert eldritch_blast.level == 0
        assert eldritch_blast.concentration is False
        assert eldritch_blast.components == "verbal, somatic"
        assert eldritch_blast.damage_die == "1d10"
        assert eldritch_blast.damage_type == "force"
        assert eldritch_blast.saving_throw is None
        assert "A beam of crackling energy" in eldritch_blast.description

        # armors
        armor_of_invulnerability = db.Armor.filter_by(name="Armor of Invulnerability", source="DMG").one()
        assert armor_of_invulnerability.rarity == Rarity.Legendary
        assert armor_of_invulnerability.requires_attunement
        assert armor_of_invulnerability.armor_class == 18

        studded_leather_armor = db.Armor.filter_by(name="Studded Leather Armor", source="PHB").one()
        assert studded_leather_armor.rarity == Rarity.Common
        assert studded_leather_armor.armor_class == 12

        # dagger
        dagger_of_venom = db.Weapon.filter_by(name="Dagger of Venom", source="DMG").one()
        assert dagger_of_venom.damage_die == "1d4"
        assert dagger_of_venom.damage_type == DamageType.piercing
        assert dagger_of_venom.light
        assert dagger_of_venom.finesse
        assert dagger_of_venom.thrown
        assert dagger_of_venom.magical
        assert dagger_of_venom.melee
        assert dagger_of_venom.ranged
        assert dagger_of_venom.attack_range == 20
        assert dagger_of_venom.attack_range_long == 60
        assert dagger_of_venom.reach == 0
        assert not dagger_of_venom.versatile
        assert not dagger_of_venom.martial

        # versatile
        quarterstaff = db.Weapon.filter_by(name="Quarterstaff", source="PHB").one()
        assert quarterstaff.versatile
        assert quarterstaff.damage_die == "1d6"
        assert quarterstaff.damage_die_two_handed == "1d8"
        assert quarterstaff.damage_type == DamageType.bludgeoning
        assert not quarterstaff.thrown
        assert not quarterstaff.two_handed

        # two-handed
        pike = db.Weapon.filter_by(name="Pike", source="PHB").one()
        assert pike.two_handed
        assert pike.damage_die == "1d10"
