from django.template.loader import render_to_string
from django.http import HttpResponse
from weasyprint import HTML
from apps.fields.models import OfficialKyosaiField, OfficialChusankanField
from apps.plans.models import Plan


def _get_plan_info(related_fields, year):
    """共済/中山間区画に紐づく圃場群から作付け情報を集約する"""
    plans = Plan.objects.filter(
        field__in=related_fields, year=year
    ).select_related('crop', 'variety', 'field')

    crop_names = []
    variety_names = []
    field_names = []

    for plan in plans:
        crop_names.append(plan.crop.name if plan.crop else '未設定')
        variety_names.append(plan.variety.name if plan.variety else '')
        field_names.append(plan.field.name)

    if not related_fields.exists():
        return {'crop': '', 'variety': '', 'field_name': ''}

    # 紐づく圃場はあるがPlanがない場合
    if not crop_names:
        names = [f.name for f in related_fields]
        return {'crop': '未設定', 'variety': '', 'field_name': ', '.join(names)}

    return {
        'crop': ', '.join(crop_names),
        'variety': ', '.join(v for v in variety_names if v),
        'field_name': ', '.join(field_names),
    }


def generate_kyosai_pdf(request, year):
    kyosai_fields = OfficialKyosaiField.objects.all().prefetch_related(
        'fields', 'fields__plans', 'fields__plans__crop', 'fields__plans__variety'
    ).order_by('k_num', 's_num')

    rows = []
    for kyosai in kyosai_fields:
        related_fields = kyosai.fields.all()
        info = _get_plan_info(related_fields, year)

        rows.append({
            'kanji_name': kyosai.kanji_name,
            'k_s_num': f"{kyosai.k_num}-{kyosai.s_num}" if kyosai.s_num else kyosai.k_num,
            'area': kyosai.area,
            'crop': info['crop'],
            'variety': info['variety'],
            'field_name': info['field_name'],
        })

    html_string = render_to_string('reports/kyosai_template.html', {
        'year': year,
        'rows': rows,
    })

    pdf = HTML(string=html_string).write_pdf()

    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = f'attachment; filename="kyosai_{year}.pdf"'
    return response


def generate_chusankan_pdf(request, year):
    chusankan_fields = OfficialChusankanField.objects.all().prefetch_related(
        'fields', 'fields__plans', 'fields__plans__crop', 'fields__plans__variety'
    ).order_by('c_id')

    rows = []
    for ch in chusankan_fields:
        related_fields = ch.fields.all()
        info = _get_plan_info(related_fields, year)

        # 所在地: 大字 + 字 + 地番 + 枝番
        location_parts = [ch.oaza, ch.aza, ch.chiban]
        if ch.branch_num and ch.branch_num != '-':
            location_parts.append(ch.branch_num)
        location = ' '.join(p for p in location_parts if p)

        rows.append({
            'location': location,
            'planting_area': ch.planting_area,
            'original_crop': ch.original_crop or '',
            'manager': ch.manager or '',
            'owner': ch.owner or '',
            'crop': info['crop'],
            'variety': info['variety'],
            'field_name': info['field_name'],
        })

    html_string = render_to_string('reports/chusankan_template.html', {
        'year': year,
        'rows': rows,
    })

    pdf = HTML(string=html_string).write_pdf()

    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = f'attachment; filename="chusankan_{year}.pdf"'
    return response
