from django.db import models


class WorkRecord(models.Model):
    class WorkType(models.TextChoices):
        FERTILIZER_DELIVERY = 'fertilizer_delivery', '肥料運搬'
        FERTILIZER_SPREADING = 'fertilizer_spreading', '肥料散布'
        LEVEE_WORK = 'levee_work', '畔塗'

    work_date = models.DateField(verbose_name='作業日')
    work_type = models.CharField(
        max_length=40,
        choices=WorkType.choices,
        verbose_name='作業種別',
    )
    title = models.CharField(max_length=200, verbose_name='タイトル')
    year = models.IntegerField(verbose_name='年度')
    auto_created = models.BooleanField(default=True, verbose_name='自動生成')
    delivery_trip = models.OneToOneField(
        'fertilizer.DeliveryTrip',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='work_record',
        verbose_name='運搬回',
    )
    spreading_session = models.OneToOneField(
        'fertilizer.SpreadingSession',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='work_record',
        verbose_name='散布実績',
    )
    levee_work_session = models.OneToOneField(
        'levee_work.LeveeWorkSession',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='work_record',
        verbose_name='畔塗記録',
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-work_date', '-updated_at', '-id']
        verbose_name = '作業記録'
        verbose_name_plural = '作業記録'

    def __str__(self):
        return f'{self.work_date} {self.get_work_type_display()}'
