from django.db import models

class Applet(models.Model):
    DASHBOARD = "dashboard"
    GAMEBOARD = "gameboard"
    WALLET = "wallet"
    BILLBOARD = "billboard"
    CONTEXT_CHOICES = [
        (DASHBOARD, "Dashboard"),
        (GAMEBOARD, "Gameboard"),
        (WALLET, "Wallet"),
        (BILLBOARD, "Billboard"),
    ]

    slug = models.SlugField(unique=True)
    name = models.CharField(max_length=100)
    context = models.CharField(max_length=20, choices=CONTEXT_CHOICES, default=DASHBOARD)
    default_visible = models.BooleanField(default=True)
    grid_cols = models.PositiveSmallIntegerField(default=12)
    grid_rows = models.PositiveSmallIntegerField(default=3)
    # Render-time sort key. Lower = earlier in the applets row. Default 100
    # gives every existing applet a tied position → falls back to PK insertion
    # order (the historical behavior), so this field is backwards-compatible.
    # Set to <100 to pin an applet ABOVE the rest (eg. wallet-shop = 10).
    display_order = models.PositiveSmallIntegerField(default=100)

    def __str__(self):
        return self.name
    
class UserApplet(models.Model):
    user = models.ForeignKey(
        "lyric.User",
        related_name="user_applets",
        on_delete=models.CASCADE,
    )
    applet = models.ForeignKey(
        Applet,
        on_delete=models.CASCADE,
    )
    visible = models.BooleanField(default=True)

    class Meta:
        unique_together = ("user", "applet")
