from django.contrib.gis.db import models as gis_models
from django.db import models


class OfficialKyosaiField(models.Model):
    k_num = models.CharField(max_length=20, verbose_name="共済番号")
    s_num = models.CharField(max_length=20, blank=True, default='', verbose_name="枝番")
    address = models.CharField(max_length=255, verbose_name="住所")
    kanji_name = models.CharField(max_length=100, verbose_name="漢字名")
    area = models.IntegerField(default=0, verbose_name="面積(m2)")

    class Meta:
        verbose_name = "共済マスタ"
        verbose_name_plural = "共済マスタ"
        unique_together = [['k_num', 's_num']]

    def __str__(self):
        return f"{self.k_num} ({self.kanji_name})"


class OfficialChusankanField(models.Model):
    c_id = models.CharField(max_length=20, unique=True, verbose_name="中山間ID")
    chusankan_flag = models.CharField(max_length=10, blank=True, null=True, verbose_name="中山間フラグ")
    oaza = models.CharField(max_length=100, verbose_name="大字")
    aza = models.CharField(max_length=100, verbose_name="字")
    chiban = models.CharField(max_length=50, verbose_name="地番")
    branch_num = models.CharField(max_length=20, blank=True, null=True, verbose_name="枝番")
    land_type = models.CharField(max_length=20, blank=True, null=True, verbose_name="地目")
    area = models.IntegerField(default=0, verbose_name="農地面積(m2)")
    planting_area = models.IntegerField(blank=True, null=True, verbose_name="植栽面積(m2)")
    original_crop = models.CharField(max_length=100, blank=True, null=True, verbose_name="作付け品目")
    manager = models.CharField(max_length=100, blank=True, null=True, verbose_name="協定管理者")
    owner = models.CharField(max_length=100, blank=True, null=True, verbose_name="所有者")
    slope = models.CharField(max_length=20, blank=True, null=True, verbose_name="傾斜度")
    base_amount = models.IntegerField(blank=True, null=True, verbose_name="基本金額")
    steep_slope_addition = models.IntegerField(blank=True, null=True, verbose_name="超急傾斜加算額")
    smart_agri_addition = models.IntegerField(blank=True, null=True, verbose_name="スマート農業加算額")
    payment_amount = models.IntegerField(blank=True, null=True, verbose_name="交付金額")

    class Meta:
        verbose_name = "中山間マスタ"
        verbose_name_plural = "中山間マスタ"

    def __str__(self):
        return f"{self.c_id} ({self.oaza}{self.aza})"


class Field(models.Model):
    name = models.CharField(max_length=100, verbose_name="圃場名")
    address = models.CharField(max_length=255, verbose_name="住所")
    area_tan = models.DecimalField(max_digits=6, decimal_places=4, verbose_name="面積(反)")
    area_m2 = models.IntegerField(verbose_name="面積(m2)")
    owner_name = models.CharField(max_length=100, verbose_name="所有者名")
    group_name = models.CharField("グループ名", max_length=50, blank=True, null=True, help_text="エリアや用途によるグループ分け")
    display_order = models.IntegerField("表示順", default=0, help_text="リスト表示時の順序")
    raw_kyosai_k_num = models.CharField("細目_耕地番号", max_length=20, null=True, blank=True)
    raw_kyosai_s_num = models.CharField("細目_分筆番号", max_length=20, null=True, blank=True)
    raw_chusankan_id = models.CharField("中山間_ID", max_length=20, null=True, blank=True)
    kyosai_fields = models.ManyToManyField(
        OfficialKyosaiField,
        blank=True,
        related_name='fields',
        verbose_name="関連共済マスタ"
    )
    chusankan_fields = models.ManyToManyField(
        OfficialChusankanField,
        blank=True,
        related_name='fields',
        verbose_name="関連中山間マスタ"
    )
    location = gis_models.PointField(blank=True, null=True, verbose_name="位置情報")

    class Meta:
        verbose_name = "圃場"
        verbose_name_plural = "圃場"

    def __str__(self):
        return self.name
