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


class Customer(models.Model):
    KINDS = [("person", "person"), ("company", "company")]
    STATUSES = [("lead", "lead"), ("active", "active"), ("inactive", "inactive")]

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="customers")
    name = models.CharField(max_length=200)
    kind = models.CharField(max_length=10, choices=KINDS, default="person")
    status = models.CharField(max_length=10, choices=STATUSES, default="lead")
    phone = models.CharField(max_length=30, blank=True, default="")
    email = models.CharField(max_length=200, blank=True, default="")
    address = models.CharField(max_length=300, blank=True, default="")
    notes = models.TextField(blank=True, default="")
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="customers_owned")
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="customers_created")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["-created_at", "-id"]
        indexes = [models.Index(fields=["organization", "status"])]


class Interaction(models.Model):
    TYPES = [("call", "call"), ("meeting", "meeting"), ("email", "email"), ("note", "note")]

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="interactions")
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name="interactions")
    type = models.CharField(max_length=10, choices=TYPES, default="note")
    summary = models.TextField()
    occurred_on = models.DateField()
    occurred_time = models.TimeField(null=True, blank=True)
    follow_up_on = models.DateField(null=True, blank=True)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="+")
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-occurred_on", "-occurred_time", "-id"]
