import uuid

from django.db import models

from apps.accounts.models import Company, TimeStampedModel


class Service(TimeStampedModel):
    """A reusable service the company offers (e.g. Bookkeeping, Tax
    Preparation). Acts as a catalog: pick one when building an invoice and its
    description, unit, rate, and tax pre-fill the line item."""

    class Category(models.TextChoices):
        BOOKKEEPING = 'bookkeeping', 'Bookkeeping'
        TAX = 'tax', 'Tax'
        ADVISORY = 'advisory', 'Advisory & Consulting'
        AUDIT = 'audit', 'Audit & Assurance'
        PAYROLL = 'payroll', 'Payroll'
        COMPLIANCE = 'compliance', 'Compliance & Secretarial'
        OTHER = 'other', 'Other'

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='services')
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    category = models.CharField(max_length=32, choices=Category.choices, default=Category.OTHER)
    # How the service is billed, e.g. "hour", "month", "return", "filing".
    default_unit = models.CharField(max_length=50, blank=True)
    default_rate = models.DecimalField(max_digits=14, decimal_places=2, default=0)
    default_tax_rate = models.DecimalField(max_digits=5, decimal_places=2, default=0)
    is_active = models.BooleanField(default=True)

    class Meta:
        ordering = ['name']

    def __str__(self):
        return self.name
