from django.db import models
from users.models import User
from organisations.models import OrganisationBranch
from ledgers.models import OrganisationSubAccount

class FixedAsset(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    payment_method = models.CharField(max_length=255)
    depreciation_rate = models.ForeignKey('DepreciationRate', on_delete=models.CASCADE, related_name='depreciation_rate_assets', null=True, blank=True )
    depreciation_method = models.CharField(max_length=255)
    deleted= models.BooleanField(default=False)
    date = models.DateTimeField()
    last_depreciation = models.DateTimeField(null=True, blank=True)
    branch = models.ForeignKey(OrganisationBranch, on_delete=models.CASCADE, related_name='branch_fixed_assets', null=True, blank=True)
    added_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_assets', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)
    debit=models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='debit_assets', null=True, blank=True)
    credit=models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='credit_assets', null=True, blank=True)
    reference_no= models.CharField(max_length=255, null=True, blank=True)
    cheque_no= models.CharField(max_length=255, null=True, blank=True)
    voucher_no= models.CharField(max_length=255, null=True, blank=True)
    depreciated_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    deprecation_dr_acct= models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='depreciation_debit_assets', null=True, blank=True)
    deprecation_cr_acct= models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='depreciation_credit_assets', null=True, blank=True)
    frequency= models.CharField(max_length=255, null=True, blank=True)
    depreciation_type= models.CharField(max_length=255, null=True, blank=True)
    class Meta:
        db_table = 'public\".\"fixed_asset'

    def __str__(self):
        return self.name
    

class DepreciationRate(models.Model):
    description = models.CharField(max_length=255)
    rate = models.DecimalField(max_digits=10, decimal_places=2)
    frequency = models.CharField(max_length=255)
    deleted= models.BooleanField(default=False)
    branch = models.ForeignKey(OrganisationBranch, on_delete=models.CASCADE, related_name='branch_fixed_depreciation_rate', null=True, blank=True)
    added_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_depreciation_rates', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)

    class Meta:
        db_table = 'public\".\"depreciation_rate'

    def __str__(self):
        return self.description
    

class FixedAssetCategory(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    added_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_fixed_asset_category', null=True, blank=True)
    deleted= models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)
    branch_id = models.ForeignKey(OrganisationBranch, on_delete=models.CASCADE, related_name='branch_fixed_asset_category', null=True, blank=True)

    class Meta:
        db_table = 'public\".\"fixed_asset_category'

    def __str__(self):
        return self.description
    

class DepreciationHistory(models.Model):
    asset_id = models.ForeignKey(FixedAsset, on_delete=models.CASCADE, related_name='asset_depreciation_history', null=True, blank=True)
    previous_value = models.DecimalField(max_digits=10, decimal_places=2)
    current_value = models.DecimalField(max_digits=10, decimal_places=2)
    description = models.TextField()
    deleted= models.BooleanField(default=False)
    added_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_depreciation_history', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)
    branch_id = models.ForeignKey(OrganisationBranch, on_delete=models.CASCADE, related_name='branch_fixed_assets_history', null=True, blank=True)

    class Meta:
        db_table = 'public\".\"depreciation_history'

    def __str__(self):
        return self.description


