from django.db import models
from osinacore.models import *

# Create your models here.

class RecurringCycle(models.Model):
    months = models.IntegerField()
    def __str__(self):
        return f"{self.months} months"


class Service(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    customer = models.ForeignKey(CustomerProfile, on_delete=models.CASCADE)
    project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
    type = models.ForeignKey(ProjectType, on_delete=models.CASCADE, blank=True)
    amount = models.DecimalField(decimal_places=3,  max_digits = 5)
    recurring = models.BooleanField(default=False)
    recurring_cycle = models.ForeignKey(RecurringCycle, on_delete=models.SET_NULL, null=True, blank=True)
    created_at = models.DateField(null=True)
    cancelled_at = models.DateField(blank=True, null=True)
    active = models.BooleanField(default=True, null=True)
    def __str__(self):
        return self.title
    

class Charge(models.Model):
    service = models.ForeignKey(Service, on_delete=models.CASCADE)
    amount =  models.DecimalField(decimal_places=3,  max_digits = 5, null=True)
    due_date = models.DateField()
    is_paid = models.BooleanField(default=False)
    def __str__(self):
        return f"{self.service}'s charge due {self.due_date}"


class PaymentType(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name


class ServicePayment(models.Model):
    charge = models.ManyToManyField(Charge, null=True)
    amount = models.DecimalField(decimal_places=3,  max_digits = 5)
    date_paid = models.DateField()
    type = models.ForeignKey(PaymentType, null=True, on_delete=models.SET_NULL)
    comment = models.TextField(null=True, blank=True)
    def __str__(self):
        return f"Payment for {self.charge}"


class Invoice(models.Model):
    invoice_number = models.CharField(max_length=100)
    charge = models.ManyToManyField(Charge)
    date_created = models.DateField()
    def __str__(self):
        return self.invoice_number


class Receipt(models.Model):
    receipt_number = models.CharField(max_length=100)
    payment = models.ManyToManyField(ServicePayment)
    date_generated = models.DateField()
    def __str__(self):
        return self.receipt_number