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

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

    LOCATION_TYPE_CHOICES = [
        ('address', 'Адрес'),
        ('hub', 'Хаб'),
    ]

    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,
    )
    selected_location_type = models.CharField(max_length=20, choices=LOCATION_TYPE_CHOICES, null=True, blank=True)
    selected_location_uuid = models.CharField(max_length=100, null=True, blank=True)
    selected_location_name = models.CharField(max_length=255, null=True, blank=True)
    selected_location_latitude = models.FloatField(null=True, blank=True)
    selected_location_longitude = models.FloatField(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}"


class TeamInvitationToken(models.Model):
    WORKFLOW_STATUS_CHOICES = [
        ('pending', 'Ожидает обработки'),
        ('active', 'Активен'),
        ('error', 'Ошибка'),
    ]

    uuid = models.CharField(max_length=100, unique=True, default=uuid.uuid4)
    invitation = models.ForeignKey(TeamInvitation, on_delete=models.CASCADE, related_name='tokens')
    token_hash = models.CharField(max_length=255, unique=True)
    workflow_status = models.CharField(
        max_length=20,
        choices=WORKFLOW_STATUS_CHOICES,
        default='pending',
    )
    expires_at = models.DateTimeField()
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'teams_invitation_token'

    def __str__(self):
        return f"Token {self.uuid} for invitation {self.invitation_id}"


class TeamAddress(models.Model):
    ADDRESS_STATUS_CHOICES = [
        ('pending', 'Ожидает обработки'),
        ('active', 'Активен'),
        ('error', 'Ошибка'),
    ]

    uuid = models.CharField(max_length=100, unique=True, default=uuid.uuid4)
    team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='addresses')
    name = models.CharField(max_length=255)  # "Офис", "Склад", "Производство"
    address = models.TextField()
    latitude = models.FloatField(null=True, blank=True)
    longitude = models.FloatField(null=True, blank=True)
    country_code = models.CharField(max_length=2, null=True, blank=True)  # ISO 3166-1 alpha-2
    is_default = models.BooleanField(default=False)
    status = models.CharField(max_length=20, choices=ADDRESS_STATUS_CHOICES, default='pending')
    processed_at = models.DateTimeField(null=True, blank=True)
    error_message = models.TextField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'teams_address'

    def __str__(self):
        return f"{self.team.name} - {self.name}"
