from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Tag(models.Model):
    name = models.CharField(max_length=50, unique=True)
    
    def __str__(self):
        return self.name

class Profile(models.Model):
    ROLE_CHOICES = [
        ('FOUNDER', 'Founder'),
        ('INVESTOR', 'Investor'),
        ('PENDING', 'Pending'),
    ]
    
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    role = models.CharField(max_length=10, choices=ROLE_CHOICES, default='PENDING')
    university = models.CharField(max_length=255, blank=True)
    graduation_year = models.IntegerField(null=True, blank=True)
    bio = models.CharField(max_length=150, blank=True)
    interests = models.ManyToManyField(Tag, blank=True)
    
    # Investor specific
    investment_appetite = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)
    investment_frequency = models.CharField(max_length=100, blank=True)
    
    # Founder specific (for partner matching)
    matching_answers = models.JSONField(null=True, blank=True) # To store questionnaire responses
    
    is_verified = models.BooleanField(default=False)
    
    def __str__(self):
        return self.user.username

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

class Startup(models.Model):
    STATUS_CHOICES = [
        ('DRAFT', 'Draft'),
        ('FUNDING', 'Funding'),
        ('FUNDED', 'Funded'),
    ]
    
    founder = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='startups')
    name = models.CharField(max_length=255)
    headline = models.CharField(max_length=255)
    description = models.TextField()
    target_amount = models.DecimalField(max_digits=12, decimal_places=2)
    raised_amount = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='DRAFT')
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.name

class Investment(models.Model):
    investor = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='investments')
    startup = models.ForeignKey(Startup, on_delete=models.CASCADE, related_name='investments')
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    timestamp = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return f"{self.investor.user.username} invested in {self.startup.name}"

class Message(models.Model):
    sender = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='sent_messages')
    receiver = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='received_messages')
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    is_read = models.BooleanField(default=False)
    
    def __str__(self):
        return f"From {self.sender.user.username} to {self.receiver.user.username}"

class Notification(models.Model):
    user = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='notifications')
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    is_read = models.BooleanField(default=False)
    
    def __str__(self):
        return f"Notification for {self.user.user.username}"