from django.db import models
from apps.fields.models import Field


class Crop(models.Model):
    name = models.CharField(max_length=100, unique=True, verbose_name="作物名")
    base_temp = models.FloatField(default=0.0, verbose_name="有効積算温度 基準温度(℃)")

    class Meta:
        verbose_name = "作物マスタ"
        verbose_name_plural = "作物マスタ"

    def __str__(self):
        return self.name


class Variety(models.Model):
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE, related_name='varieties', verbose_name="作物")
    name = models.CharField(max_length=100, verbose_name="品種名")

    class Meta:
        verbose_name = "品種マスタ"
        verbose_name_plural = "品種マスタ"
        unique_together = [['crop', 'name']]

    def __str__(self):
        return f"{self.crop.name} - {self.name}"


class Plan(models.Model):
    field = models.ForeignKey(Field, on_delete=models.CASCADE, related_name='plans', verbose_name="圃場")
    year = models.IntegerField(verbose_name="作付年度")
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE, related_name='plans', verbose_name="作物")
    variety = models.ForeignKey(Variety, on_delete=models.SET_NULL, related_name='plans', verbose_name="品種", blank=True, null=True)
    notes = models.TextField(blank=True, null=True, verbose_name="備考")

    class Meta:
        verbose_name = "作付け計画"
        verbose_name_plural = "作付け計画"
        unique_together = [['field', 'year']]
        ordering = ['-year', 'field']

    def __str__(self):
        return f"{self.field.name} - {self.year} - {self.crop.name}"
