from django.db import models
from django.conf import settings


class Resume(models.Model):
    GENDER_CHOICES = [('male', '男'), ('female', '女'), ('other', '其他')]
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='resume'
    )
    name = models.CharField(max_length=50, verbose_name='姓名')
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES, blank=True)
    birthday = models.DateField(null=True, blank=True)
    education = models.JSONField(default=list, verbose_name='教育经历')
    experience = models.JSONField(default=list, verbose_name='工作经历')
    attachment = models.FileField(upload_to='resumes/', null=True, blank=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '简历'

    def to_snapshot(self):
        """序列化为投递快照，与主表解耦"""
        attachment_url = None
        if self.attachment:
            # 返回相对 URL，前端会处理
            attachment_url = self.attachment.url

        return {
            'name': self.name,
            'gender': self.gender,
            'birthday': str(self.birthday) if self.birthday else None,
            'education': self.education,
            'experience': self.experience,
            'email': self.user.email,
            'phone': self.user.phone,
            'attachment_url': attachment_url,
        }
