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


class Project(models.Model):
    STATUS_CHOICES = [
        ('Active', 'Active'),
        ('On_Hold', 'On Hold'),
        ('Completed', 'Completed'),
        ('Cancelled', 'Cancelled'),
    ]

    STAGE_CHOICES = [
        ('Kickoff', 'Kickoff'),
        ('In_Progress', 'In Progress'),
        ('Internal_Review', 'Internal Review'),
        ('Client_Delivery', 'Client Delivery'),
        ('Completed', 'Completed'),
    ]

    lead = models.OneToOneField(
        'leads.Lead', on_delete=models.SET_NULL, null=True, blank=True,
        related_name='project'
    )
    name = models.CharField(max_length=255)
    client_name = models.CharField(max_length=255)
    service_type = models.CharField(max_length=10, default='Tech')
    approved_budget = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal('0.00'))
    estimated_cost = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal('0.00'))
    actual_cost = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal('0.00'))
    start_date = models.DateField(null=True, blank=True)
    expected_end_date = models.DateField(null=True, blank=True)
    pm = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.SET_NULL,
        null=True, related_name='managed_projects'
    )
    status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='Active')
    stage = models.CharField(max_length=20, choices=STAGE_CHOICES, default='Kickoff')

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        app_label = 'projects'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.name} ({self.client_name})'


class ProjectMember(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='members')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='project_memberships')
    role_on_project = models.CharField(max_length=100)
    hourly_rate_snapshot = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))

    class Meta:
        app_label = 'projects'
        unique_together = ['project', 'user']

    def __str__(self):
        return f'{self.user.full_name} on {self.project.name}'


class ProjectAttachment(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='attachments')
    file = models.FileField(upload_to='project_attachments/')
    file_name = models.CharField(max_length=255, blank=True)
    uploaded_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
    uploaded_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        app_label = 'projects'

    def __str__(self):
        return f'{self.file_name} ({self.project.name})'


class ProjectStageLog(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='stage_logs')
    from_stage = models.CharField(max_length=20)
    to_stage = models.CharField(max_length=20)
    note = models.TextField(blank=True)
    changed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        app_label = 'projects'
        ordering = ['-timestamp']

    def __str__(self):
        return f'{self.project.name}: {self.from_stage} → {self.to_stage}'
