from django.db import models
from django.contrib.auth.models import User

class MemberOfParliament(models.Model):
    name = models.CharField(max_length=255)
    party = models.CharField(max_length=100)
    constituency = models.CharField(max_length=255)
    province = models.CharField(max_length=100)
    image_url = models.URLField(blank=True, null=True)
    
    def __str__(self):
        return self.name

class TradeDisclosure(models.Model):
    TRADE_TYPES = (
        ('BUY', 'Buy'),
        ('SELL', 'Sell'),
        ('HOLD', 'Hold'),
    )
    
    mp = models.ForeignKey(MemberOfParliament, on_delete=models.CASCADE, related_name='trades')
    ticker = models.CharField(max_length=20)
    company_name = models.CharField(max_length=255)
    trade_type = models.CharField(max_length=10, choices=TRADE_TYPES)
    amount_range = models.CharField(max_length=100) # e.g. "$15,001 - $50,000"
    disclosure_date = models.DateField()
    transaction_date = models.DateField(null=True, blank=True)
    
    def __str__(self):
        return f"{self.mp.name} - {self.ticker} ({self.trade_type})"

class Watchlist(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='watchlist')
    mp = models.ForeignKey(MemberOfParliament, on_delete=models.CASCADE, blank=True, null=True)
    ticker = models.CharField(max_length=20, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'mp', 'ticker')