from django.test import TestCase

from apps.fields.models import Field
from apps.plans.models import Crop, Plan, Variety

from .models import LeveeWorkSession, LeveeWorkSessionItem
from .serializers import LeveeWorkSessionSerializer


class LeveeWorkSessionSerializerTests(TestCase):
    def test_total_area_tan_is_included(self):
        crop = Crop.objects.create(name='水稲')
        variety = Variety.objects.create(crop=crop, name='にこまる')
        field_a = Field.objects.create(
            name='足川北上',
            address='高知県高岡郡',
            area_tan='1.2000',
            area_m2=1200,
            owner_name='吉田',
            group_name='北',
            display_order=1,
        )
        field_b = Field.objects.create(
            name='足川南',
            address='高知県高岡郡',
            area_tan='0.8000',
            area_m2=800,
            owner_name='吉田',
            group_name='南',
            display_order=2,
        )
        plan_a = Plan.objects.create(field=field_a, year=2026, crop=crop, variety=variety, notes='')
        plan_b = Plan.objects.create(field=field_b, year=2026, crop=crop, variety=variety, notes='')
        session = LeveeWorkSession.objects.create(
            year=2026,
            date='2026-04-06',
            title='水稲畔塗',
            notes='',
        )
        LeveeWorkSessionItem.objects.create(
            session=session,
            field=field_a,
            plan=plan_a,
            crop_name_snapshot='水稲',
            variety_name_snapshot='にこまる',
        )
        LeveeWorkSessionItem.objects.create(
            session=session,
            field=field_b,
            plan=plan_b,
            crop_name_snapshot='水稲',
            variety_name_snapshot='にこまる',
        )

        data = LeveeWorkSessionSerializer(session).data

        self.assertEqual(data['item_count'], 2)
        self.assertEqual(data['total_area_tan'], '2.0000')
