import sys
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from app.services.tagging import curate_recipe_tags
from app.services.recipe_importer import normalize_recipe_from_url, parse_duration_minutes


class TaggingTests(unittest.TestCase):
    def test_textual_durations_cover_weissman_style_time_strings(self):
        self.assertEqual(parse_duration_minutes('1 hr, 15 min'), 75)
        self.assertEqual(parse_duration_minutes('45 min + 12 hrs'), 765)
        self.assertEqual(parse_duration_minutes('1 day + 30 min'), 1470)
        self.assertEqual(parse_duration_minutes('1 hr + overnight'), 780)
        self.assertEqual(parse_duration_minutes('1 hr 30 min + overnight'), 810)
        self.assertEqual(parse_duration_minutes('30 minutes'), 30)
        self.assertEqual(parse_duration_minutes('90 sec'), 2)

    def test_tater_tots_drop_boilerplate_and_gain_meaningful_tags(self):
        payload = normalize_recipe_from_url(
            'https://www.joshuaweissman.com/recipes/the-best-tater-tots-ever-recipe'
        )

        self.assertNotIn('the-best-tater-tots-ever', payload['tags'])
        self.assertNotIn('chef-joshua-weissman-recipe', payload['tags'])
        self.assertIn('potato', payload['tags'])
        self.assertIn('tater-tots', payload['tags'])
        self.assertIn('side-dish', payload['tags'])
        self.assertNotIn('beef', payload['tags'])
        self.assertEqual(payload['prep_time'], 30)
        self.assertEqual(payload['cook_time'], 45)
        self.assertEqual(payload['time'], 75)

    def test_halal_chicken_rice_drops_boilerplate_and_infers_cuisine(self):
        payload = normalize_recipe_from_url(
            'https://www.joshuaweissman.com/recipes/halal-guys-chicken-over-rice-but-better-recipe'
        )

        self.assertNotIn('halal-guys-chicken-over-rice-but-better', payload['tags'])
        self.assertNotIn('chef-joshua-weissman-recipe', payload['tags'])
        self.assertIn('middle-eastern', payload['tags'])
        self.assertIn('chicken', payload['tags'])
        self.assertIn('rice', payload['tags'])
        self.assertNotIn('skillet', payload['tags'])
        self.assertEqual(payload['prep_time'], 30)
        self.assertEqual(payload['cook_time'], 45)
        self.assertEqual(payload['time'], 75)

    def test_existing_machine_tags_can_be_replaced_when_recipe_content_disagrees(self):
        tags = curate_recipe_tags(
            name='The Best Tater Tots Ever',
            description='Homemade tater tots worth making. Fry until crisp.',
            items=[
                {'name': 'Russet potatoes', 'description': 'peeled', 'optional': False},
                {'name': 'beef tallow', 'description': 'for frying', 'optional': False},
            ],
            source='https://www.joshuaweissman.com/recipes/the-best-tater-tots-ever-recipe',
            existing_tags=['beef', 'main-course', 'savory'],
        )

        self.assertNotIn('beef', tags)
        self.assertNotIn('main-course', tags)
        self.assertIn('side-dish', tags)
        self.assertIn('savory', tags)

    def test_existing_human_curated_tags_are_preserved(self):
        tags = curate_recipe_tags(
            name='Simple Roast Chicken',
            description='A chicken dinner with herbs.',
            items=[{'name': 'chicken', 'description': '', 'optional': False}],
            source='https://example.com/roast-chicken',
            existing_tags=['family-favorite', 'weeknight'],
        )

        self.assertIn('family-favorite', tags)
        self.assertIn('weeknight', tags)

    def test_mango_sticky_rice_is_treated_as_dessert_not_savory_main(self):
        tags = curate_recipe_tags(
            name='Perfect Mango Sticky Rice At Home',
            description='Mango sticky rice is something everyone can, and should, make.',
            items=[
                {'name': 'glutinous rice', 'description': '1.5 cups', 'optional': False},
                {'name': 'full-fat coconut milk', 'description': '1 can', 'optional': False},
                {'name': 'mangos', 'description': '2', 'optional': False},
                {'name': 'granulated sugar', 'description': '1/2 cup', 'optional': False},
            ],
            source='https://www.joshuaweissman.com/recipes/perfect-mango-sticky-rice-recipe',
            existing_tags=['chef-joshua-weissman-recipe', 'main-course', 'savory'],
        )

        self.assertIn('dessert', tags)
        self.assertIn('sweet', tags)
        self.assertIn('thai', tags)
        self.assertNotIn('main-course', tags)
        self.assertNotIn('savory', tags)

    def test_description_only_chicken_reference_does_not_force_chicken_tag(self):
        tags = curate_recipe_tags(
            name='Quick Asian Beef Ramen Noodles',
            description='Try the chicken version next time if you want a variation.',
            items=[
                {'name': 'ground beef', 'description': '200g', 'optional': False},
                {'name': 'ramen noodles', 'description': '2 packs', 'optional': False},
                {'name': 'soy sauce', 'description': '1 tbsp', 'optional': False},
            ],
            source='https://www.recipetineats.com/asian-beef-and-noodles/',
            existing_tags=['asian', 'noodles'],
        )

        self.assertIn('beef', tags)
        self.assertNotIn('chicken', tags)

    def test_pasta_chips_are_snack_not_main_course(self):
        tags = curate_recipe_tags(
            name='Air Fryer Pasta Chips',
            description='This could be one of the greatest homemade snacks of all time.',
            items=[
                {'name': 'farfalle pasta', 'description': '8 oz', 'optional': False},
                {'name': 'pecorino romano', 'description': 'to taste', 'optional': False},
                {'name': 'olive oil', 'description': 'drizzle', 'optional': False},
            ],
            source='https://www.joshuaweissman.com/recipes/air-fryer-pasta-chips-recipe',
            existing_tags=['italian', 'main-course', 'pasta', 'savory'],
        )

        self.assertIn('snack', tags)
        self.assertIn('pasta', tags)
        self.assertNotIn('main-course', tags)


if __name__ == '__main__':
    unittest.main()
