from django.conf import settings
from django.db import models
import uuid

class Team(models.Model):
    TEAM_STATUS_CHOICES = [
        ('PENDING_KYC', 'Требуется KYC'),
        ('KYC_IN_REVIEW', 'KYC на рассмотрении'),
        ('KYC_APPROVED', 'KYC одобрен'),
        ('KYC_REJECTED', 'KYC отклонен'),
        ('SUSPENDED', 'Заблокировано'),
    ]

    TEAM_TYPE_CHOICES = [
        ('BUYER', 'Покупатель'),
        ('SELLER', 'Продавец'),
    ]

    uuid = models.CharField(max_length=100, unique=True, default=uuid.uuid4)
    name = models.CharField(max_length=255)
    team_type = models.CharField(max_length=20, choices=TEAM_TYPE_CHOICES, default='BUYER')
    logto_org_id = models.CharField(max_length=255, null=True, blank=True)
    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='owned_teams',
        null=True,
        blank=True,
    )
    status = models.CharField(max_length=50, choices=TEAM_STATUS_CHOICES, default='PENDING_KYC')
    prefect_flow_run_id = models.CharField(max_length=255, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'teams_team'
        
    def __str__(self):
        return f"Team {self.name}"


class UserProfile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='profile'
    )
    logto_id = models.CharField(max_length=255, unique=True)
    avatar_id = models.CharField(max_length=100, blank=True, null=True)
    phone = models.CharField(max_length=20, blank=True, null=True)
    active_team = models.ForeignKey(Team, on_delete=models.SET_NULL, null=True, blank=True, related_name='active_profiles')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'teams_user_profile'

    def __str__(self):
        return f"Profile {self.logto_id}"

class TeamMember(models.Model):
    ROLE_CHOICES = [
        ('OWNER', 'Владелец'),
        ('ADMIN', 'Администратор'),
        ('MANAGER', 'Менеджер'),
        ('MEMBER', 'Участник'),
    ]
    
    uuid = models.CharField(max_length=100, unique=True, default=uuid.uuid4)
    team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='members')
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='team_memberships',
        null=True,
        blank=True,
    )
    role = models.CharField(max_length=50, choices=ROLE_CHOICES, default='MEMBER')
    joined_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'teams_member'
        unique_together = ['team', 'user']
        
    def __str__(self):
        return f"{self.team.name} - {self.user} ({self.role})"

class TeamInvitation(models.Model):
    INVITATION_STATUS_CHOICES = [
        ('PENDING', 'Ожидает ответа'),
        ('ACCEPTED', 'Принято'),
        ('DECLINED', 'Отклонено'),
        ('EXPIRED', 'Истекло'),
    ]
    
    ROLE_CHOICES = TeamMember.ROLE_CHOICES
    
    uuid = models.CharField(max_length=100, unique=True, default=uuid.uuid4)
    team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='invitations')
    email = models.EmailField()
    role = models.CharField(max_length=50, choices=ROLE_CHOICES, default='MEMBER')
    status = models.CharField(max_length=50, choices=INVITATION_STATUS_CHOICES, default='PENDING')
    invited_by = models.CharField(max_length=255)
    expires_at = models.DateTimeField()
    created_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        db_table = 'teams_invitation'
        unique_together = ['team', 'email']
        
    def __str__(self):
        return f"Приглашение в {self.team.name} для {self.email}"
