from django.db import models
from django.utils import timezone
from django.conf import settings
from organisations.models import Organisation
from django.db.models import JSONField


class CreditEnquiry(models.Model):
    """Cache for credit enquiry requests and responses"""
    organisation = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
        related_name='credit_enquiries'
    )
    identifier = models.CharField(
        max_length=255,
        help_text="Identifier (NIN) of the member being searched"
    )
    entity_type = models.IntegerField(
        help_text="0 for individual, 1 for non-individual"
    )
    identification_type = models.CharField(max_length=255)
    reason = models.CharField(max_length=500)
    client_consented = models.CharField(max_length=50)
    sector = models.CharField(max_length=255, null=True, blank=True)
    format = models.CharField(max_length=50, null=True, blank=True)
    pi_code = models.CharField(max_length=255, null=True, blank=True)
    
    # API Response data stored as JSON
    api_response = JSONField(default=dict, blank=True)
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'crb_credit_enquiry'
        # Unique combination of organisation, identifier, and entity_type
        unique_together = ('organisation', 'identifier', 'entity_type')
        indexes = [
            models.Index(fields=['organisation', 'identifier']),
            models.Index(fields=['created_at']),
        ]
    
    def __str__(self):
        return f"CreditEnquiry - {self.organisation.name} - {self.identifier}"


class CreditReport(models.Model):
    """Cache for credit report requests and responses"""
    organisation = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
        related_name='credit_reports'
    )
    enquiry_id = models.CharField(max_length=255)
    identifier = models.CharField(
        max_length=255,
        help_text="Identifier (NIN) of the member being searched"
    )
    entity_type = models.IntegerField(
        help_text="0 for individual, 1 for non-individual"
    )
    format = models.CharField(max_length=50)
    individual_id = models.CharField(max_length=255, null=True, blank=True)
    non_individual_id = models.CharField(max_length=255, null=True, blank=True)
    
    # API Response data stored as JSON
    api_response = JSONField(default=dict, blank=True)
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'crb_credit_report'
        # Unique combination of organisation, enquiry_id, and identifier
        unique_together = ('organisation', 'enquiry_id', 'identifier')
        indexes = [
            models.Index(fields=['organisation', 'identifier']),
            models.Index(fields=['enquiry_id']),
            models.Index(fields=['created_at']),
        ]
    
    def __str__(self):
        return f"CreditReport - {self.organisation.name} - {self.enquiry_id}"


class KYCEnquiry(models.Model):
    """Cache for KYC enquiry requests and responses"""
    organisation = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
        related_name='kyc_enquiries'
    )
    identifier = models.CharField(
        max_length=255,
        help_text="Identifier (NIN) of the member being searched"
    )
    entity_type = models.IntegerField(
        help_text="0 for individual, 1 for non-individual"
    )
    identification_type = models.CharField(max_length=255)
    reason = models.CharField(max_length=500)
    client_consented = models.CharField(max_length=50)
    sector = models.CharField(max_length=255, null=True, blank=True)
    format = models.CharField(max_length=50, null=True, blank=True)
    pi_code = models.CharField(max_length=255, null=True, blank=True)
    
    # API Response data stored as JSON
    api_response = JSONField(default=dict, blank=True)
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'crb_kyc_enquiry'
        # Unique combination of organisation, identifier, and entity_type
        unique_together = ('organisation', 'identifier', 'entity_type')
        indexes = [
            models.Index(fields=['organisation', 'identifier']),
            models.Index(fields=['created_at']),
        ]
    
    def __str__(self):
        return f"KYCEnquiry - {self.organisation.name} - {self.identifier}"


class KYCReport(models.Model):
    """Cache for KYC report requests and responses"""
    organisation = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
        related_name='kyc_reports'
    )
    enquiry_id = models.CharField(max_length=255)
    identifier = models.CharField(
        max_length=255,
        help_text="Identifier (NIN) of the member being searched"
    )
    entity_type = models.IntegerField(
        help_text="0 for individual, 1 for non-individual"
    )
    format = models.CharField(max_length=50)
    individual_id = models.CharField(max_length=255, null=True, blank=True)
    non_individual_id = models.CharField(max_length=255, null=True, blank=True)
    
    # API Response data stored as JSON
    api_response = JSONField(default=dict, blank=True)
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'crb_kyc_report'
        # Unique combination of organisation, enquiry_id, and identifier
        unique_together = ('organisation', 'enquiry_id', 'identifier')
        indexes = [
            models.Index(fields=['organisation', 'identifier']),
            models.Index(fields=['enquiry_id']),
            models.Index(fields=['created_at']),
        ]
    
    def __str__(self):
        return f"KYCReport - {self.organisation.name} - {self.enquiry_id}"