from django.db import models


class Job(models.Model):
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('published', '已发布'),
        ('closed', '已关闭'),
    ]
    EDUCATION_CHOICES = [
        ('博士', '博士'),
        ('硕士', '硕士'),
        ('本科及以下', '本科及以下'),
    ]
    organization = models.ForeignKey(
        'organizations.Organization',
        on_delete=models.CASCADE,
        related_name='jobs'
    )
    title = models.CharField(max_length=100, verbose_name='职位名称')
    category = models.CharField(max_length=50, verbose_name='职位类别')
    location = models.CharField(max_length=100, verbose_name='工作地点')
    salary = models.CharField(max_length=50, verbose_name='薪资范围')
    education = models.CharField(max_length=20, choices=EDUCATION_CHOICES, default='本科及以下', verbose_name='学历要求')
    description = models.TextField(verbose_name='职位描述', blank=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-created_at']
        verbose_name = '职位'

    def __str__(self):
        return self.title


class JobFavorite(models.Model):
    user = models.ForeignKey(
        'accounts.User', on_delete=models.CASCADE, related_name='job_favorites'
    )
    job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name='favorited_by')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'job')
        ordering = ['-created_at']
