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

from apps.core.models import TimeStampedModel


class InvestorType(models.TextChoices):
    INDIVIDUAL = "individual", "Individual"
    CORPORATE = "corporate", "Corporate"


class ApplicationStatus(models.TextChoices):
    PENDING = "pending", "Pending review"
    APPROVED = "approved", "Approved"
    REJECTED = "rejected", "Rejected"
    MORE_INFO = "more_info", "More information required"


class InvestorStatus(models.TextChoices):
    PENDING = "pending", "Pending"
    ACTIVE = "active", "Active"
    SUSPENDED = "suspended", "Suspended"
    CLOSED = "closed", "Closed"


class KYCStatus(models.TextChoices):
    UNVERIFIED = "unverified", "Unverified"
    PENDING = "pending", "Pending"
    VERIFIED = "verified", "Verified"
    REJECTED = "rejected", "Rejected"


class InvestorApplication(TimeStampedModel):
    investor_type = models.CharField(max_length=20, choices=InvestorType.choices)
    status = models.CharField(max_length=20, choices=ApplicationStatus.choices, default=ApplicationStatus.PENDING)
    full_name = models.CharField(max_length=180, blank=True)
    country = models.CharField(max_length=120, blank=True)
    nationality = models.CharField(max_length=120, blank=True)
    passport_id = models.CharField(max_length=80, blank=True)
    email = models.EmailField()
    mobile = models.CharField(max_length=40, blank=True)
    address = models.TextField(blank=True)
    investment_interest = models.CharField(max_length=80, blank=True)
    preferred_project_slug = models.CharField(max_length=120, blank=True)
    investment_range = models.CharField(max_length=80, blank=True)
    company_name = models.CharField(max_length=180, blank=True)
    registration_number = models.CharField(max_length=80, blank=True)
    authorized_person = models.CharField(max_length=180, blank=True)
    business_info = models.TextField(blank=True)
    investment_capacity = models.CharField(max_length=80, blank=True)
    review_notes = models.TextField(blank=True)
    reviewed_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="reviewed_applications",
    )
    reviewed_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return f"{self.email} ({self.investor_type})"


class Investor(TimeStampedModel):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="investor")
    account_code = models.CharField(max_length=32, unique=True)
    investor_type = models.CharField(max_length=20, choices=InvestorType.choices, default=InvestorType.INDIVIDUAL)
    status = models.CharField(max_length=20, choices=InvestorStatus.choices, default=InvestorStatus.PENDING)
    kyc_status = models.CharField(max_length=20, choices=KYCStatus.choices, default=KYCStatus.UNVERIFIED)
    country = models.CharField(max_length=120, blank=True)
    nationality = models.CharField(max_length=120, blank=True)
    passport_id_last4 = models.CharField(max_length=8, blank=True)
    address = models.TextField(blank=True)
    company_name = models.CharField(max_length=180, blank=True)
    registration_number = models.CharField(max_length=80, blank=True)
    application = models.OneToOneField(
        InvestorApplication,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="investor",
    )

    class Meta:
        ordering = ["account_code"]

    def __str__(self):
        return f"{self.account_code} · {self.user.email}"


class BeneficiaryCategory(models.TextChoices):
    INVESTOR = "investor", "Investor"
    EMPLOYEE = "employee", "Employee"
    PARTNER = "partner", "Partner"
    COMMUNITY = "community", "Community"


class BenefitType(models.TextChoices):
    INVESTMENT = "investment", "Investment Benefit"
    INCENTIVE = "incentive", "Incentive"
    COMMISSION = "commission", "Commission"
    COMMUNITY = "community", "Community Benefit"


class BeneficiaryStatus(models.TextChoices):
    ACTIVE = "active", "Active"
    PAID = "paid", "Paid"
    PENDING = "pending", "Pending"
    CLOSED = "closed", "Closed"


class Beneficiary(TimeStampedModel):
    investor = models.ForeignKey(Investor, null=True, blank=True, on_delete=models.SET_NULL, related_name="beneficiaries")
    project = models.ForeignKey("projects.Project", on_delete=models.CASCADE, related_name="beneficiaries")
    display_name = models.CharField(max_length=180)
    public_label = models.CharField(max_length=180, blank=True)
    category = models.CharField(max_length=20, choices=BeneficiaryCategory.choices)
    benefit_type = models.CharField(max_length=20, choices=BenefitType.choices)
    status = models.CharField(max_length=20, choices=BeneficiaryStatus.choices, default=BeneficiaryStatus.ACTIVE)
    is_public = models.BooleanField(default=False)

    class Meta:
        ordering = ["display_name"]
        verbose_name_plural = "Beneficiaries"

    def __str__(self):
        return self.display_name
