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

from apps.core.models import Currency, TimeStampedModel


class InvestmentStatus(models.TextChoices):
    PENDING = "pending", "Pending"
    PAYMENT_PENDING = "payment_pending", "Payment pending"
    PENDING_VERIFICATION = "pending_verification", "Pending verification"
    ACTIVE = "active", "Active"
    COMPLETED = "completed", "Completed"
    CANCELLED = "cancelled", "Cancelled"


class InvestmentSource(models.TextChoices):
    STAFF = "staff", "Staff entered"
    INVESTOR = "investor", "Investor order"


class Investment(TimeStampedModel):
    investor = models.ForeignKey("investors.Investor", on_delete=models.CASCADE, related_name="investments")
    project = models.ForeignKey("projects.Project", on_delete=models.PROTECT, related_name="investments")
    agreement_code = models.CharField(max_length=64, unique=True)
    reference = models.CharField(max_length=32, unique=True, blank=True, null=True)
    status = models.CharField(max_length=24, choices=InvestmentStatus.choices, default=InvestmentStatus.PENDING)
    source = models.CharField(max_length=16, choices=InvestmentSource.choices, default=InvestmentSource.STAFF)
    currency = models.CharField(max_length=8, choices=Currency.choices, default=Currency.BDT)
    committed_amount = models.DecimalField(max_digits=16, decimal_places=2)
    received_amount = models.DecimalField(max_digits=16, decimal_places=2, default=0)
    expected_benefit = models.DecimalField(max_digits=16, decimal_places=2, default=0)
    benefit_received = models.DecimalField(max_digits=16, decimal_places=2, default=0)
    participation_percent = models.DecimalField(max_digits=8, decimal_places=4, default=0)
    profit_share_percent = models.DecimalField(max_digits=8, decimal_places=4, default=0)
    next_distribution_on = models.DateField(null=True, blank=True)
    agreement_executed_on = models.DateField(null=True, blank=True)
    agreement_accepted_at = models.DateTimeField(null=True, blank=True)
    payment_method = models.CharField(max_length=40, blank=True)
    payment_reference = models.CharField(max_length=80, blank=True)
    payment_verified_at = models.DateTimeField(null=True, blank=True)
    payment_verified_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="verified_investments",
    )
    activated_at = models.DateTimeField(null=True, blank=True)
    notes = models.TextField(blank=True)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return f"{self.agreement_code} · {self.investor.account_code}"


class InvestmentAllocation(TimeStampedModel):
    investment = models.ForeignKey(Investment, on_delete=models.CASCADE, related_name="allocations")
    label = models.CharField(max_length=120)
    percent = models.DecimalField(max_digits=5, decimal_places=2)
    amount = models.DecimalField(max_digits=16, decimal_places=2, default=0)

    class Meta:
        ordering = ["id"]


class InvestmentCertificate(TimeStampedModel):
    investment = models.OneToOneField(Investment, on_delete=models.CASCADE, related_name="certificate")
    certificate_number = models.CharField(max_length=40, unique=True)
    issued_at = models.DateTimeField(auto_now_add=True)
    document = models.ForeignKey(
        "documents.Document",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="certificates",
    )

    def __str__(self):
        return self.certificate_number
