from django.db import models


class ContactMessage(models.Model):
    """Inbound message from the public contact form."""

    class Role(models.TextChoices):
        SEEKER = 'seeker', 'Job Seeker'
        EMPLOYER = 'employer', 'Employer / Company'
        RECRUITER = 'recruiter', 'Recruiter'
        MEDIA = 'media', 'Media / Press'
        OTHER = 'other', 'Other'

    class Subject(models.TextChoices):
        GENERAL = 'general', 'General Inquiry'
        BILLING = 'billing', 'Subscription / Billing'
        SUPPORT = 'support', 'Technical Support'
        PARTNERSHIP = 'partnership', 'Partnerships'
        TRAINING = 'training', 'Corporate Training'
        RECRUITMENT = 'recruitment', 'Recruitment'
        ADVERTISE = 'advertise', 'Advertise with PEZAJOB'
        REPORT = 'report', 'Report a Problem'
        MEDIA = 'media', 'Media Inquiry'

    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=30, blank=True)
    company = models.CharField(max_length=150, blank=True)
    position = models.CharField(max_length=120, blank=True)
    organisation_type = models.CharField(max_length=40, blank=True)
    partnership_areas = models.TextField(blank=True)
    website = models.CharField(max_length=255, blank=True)
    sender_role = models.CharField(max_length=20, choices=Role.choices)
    subject = models.CharField(max_length=30, choices=Subject.choices)
    message = models.TextField()
    is_read = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']
        verbose_name = 'contact message'
        verbose_name_plural = 'contact messages'

    def __str__(self):
        return f'{self.first_name} {self.last_name} - {self.get_subject_display()}'

    @property
    def full_name(self):
        return f'{self.first_name} {self.last_name}'.strip()


class DailyVisitorCount(models.Model):
    """Daily site visitor totals from legacy accounts_visitortraffic."""
    day = models.DateField(unique=True)
    count = models.PositiveIntegerField(default=0)
    updated_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ['-day']
        verbose_name = 'daily visitor count'
        verbose_name_plural = 'daily visitor counts'

    def __str__(self):
        return f'{self.day}: {self.count}'


class SiteContact(models.Model):
    """Legacy contact/inquiry submissions from chat_contact."""
    full_name = models.CharField(max_length=120)
    email = models.EmailField(blank=True)
    phone = models.CharField(max_length=30, blank=True)
    country = models.CharField(max_length=80, blank=True)
    company = models.CharField(max_length=120, blank=True)
    department = models.CharField(max_length=120, blank=True)
    item_type = models.CharField(max_length=80, blank=True)
    message = models.TextField(blank=True)
    link = models.URLField(blank=True)
    user = models.ForeignKey(
        'accounts.User',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='site_contacts',
    )
    created_at = models.DateTimeField()

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.full_name} ({self.email})'
