from django.conf import settings
from django.db import models


class ApplicationNote(models.Model):
    application = models.ForeignKey(
        'jobs.JobApplication',
        on_delete=models.CASCADE,
        related_name='notes',
    )
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        related_name='application_notes',
    )
    body = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f'Note on {self.application_id} by {self.author}'


class ApplicationEvent(models.Model):
    class EventType(models.TextChoices):
        CREATED = 'created', 'Application submitted'
        STATUS_CHANGED = 'status_changed', 'Status changed'
        NOTE_ADDED = 'note_added', 'Note added'
        INTERVIEW_SCHEDULED = 'interview_scheduled', 'Interview scheduled'
        INTERVIEW_UPDATED = 'interview_updated', 'Interview updated'
        RATING_SET = 'rating_set', 'Rating updated'

    application = models.ForeignKey(
        'jobs.JobApplication',
        on_delete=models.CASCADE,
        related_name='events',
    )
    actor = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='application_events',
    )
    event_type = models.CharField(max_length=30, choices=EventType.choices)
    old_value = models.CharField(max_length=120, blank=True)
    new_value = models.CharField(max_length=120, blank=True)
    message = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.get_event_type_display()} – app {self.application_id}'


class Interview(models.Model):
    class Status(models.TextChoices):
        SCHEDULED = 'scheduled', 'Scheduled'
        COMPLETED = 'completed', 'Completed'
        CANCELLED = 'cancelled', 'Cancelled'

    application = models.ForeignKey(
        'jobs.JobApplication',
        on_delete=models.CASCADE,
        related_name='interviews',
    )
    scheduled_at = models.DateTimeField()
    location = models.CharField(max_length=200, blank=True)
    meeting_url = models.URLField(blank=True)
    notes = models.TextField(blank=True)
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.SCHEDULED)
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        related_name='scheduled_interviews',
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['scheduled_at']

    def __str__(self):
        return f'Interview for {self.application} at {self.scheduled_at}'
