from django.db import models
from django.contrib.auth.models import User
from hub.services.models import ServiceOffering


class ServiceBrokerUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    allowed_offerings = models.ManyToManyField(ServiceOffering, blank=True)

    def __str__(self):
        return f"Broker User: {self.user.username}"


class ServiceInstance(models.Model):
    instance_id = models.CharField(max_length=100, unique=True)
    offering = models.ForeignKey("services.ServiceOffering", on_delete=models.PROTECT)
    plan = models.ForeignKey("services.Plan", on_delete=models.PROTECT)
    organization_guid = models.CharField(max_length=100)
    space_guid = models.CharField(max_length=100)
    organization_name = models.CharField(max_length=200)
    parameters = models.JSONField(default=dict)
    context = models.JSONField(default=dict)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    lead = models.ForeignKey("services.Lead", on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return f"{self.offering.service.name} on {self.offering.cloud_provider.name} - {self.instance_id}"
