from django.db import models
from django.urls import reverse

class Artist(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    description = models.TextField()
    image_url = models.URLField(blank=True, null=True)
    style = models.CharField(max_length=100, help_text="e.g. Rock, Metal, Blues")

    def __str__(self):
        return self.name

class EffectModule(models.Model):
    CATEGORY_CHOICES = [
        ('dynamics', 'Dynamics (Gate/Comp)'),
        ('drive', 'Drive/Overdrive'),
        ('amp', 'Amplifier'),
        ('modulation', 'Modulation (Chorus/Flanger)'),
        ('delay_reverb', 'Delay/Reverb'),
        ('utility', 'Utility (IR Loader/Wah)'),
        ('cab', 'Cabinet'),
    ]
    name = models.CharField(max_length=255)
    category = models.CharField(max_length=50, choices=CATEGORY_CHOICES)
    description = models.TextField(blank=True)
    icon_class = models.CharField(max_length=50, default='bi-cpu', help_text="Bootstrap Icon class")

    def __str__(self):
        return self.name

class Preset(models.Model):
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE, related_name='presets')
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    description = models.TextField()
    is_featured = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.artist.name} - {self.title}"

class SignalChainStep(models.Model):
    preset = models.ForeignKey(Preset, on_delete=models.CASCADE, related_name='steps')
    module = models.ForeignKey(EffectModule, on_delete=models.CASCADE)
    order = models.PositiveIntegerField(default=0)
    settings_summary = models.CharField(max_length=255, blank=True, help_text="e.g. Gain: 8, Bass: 4")

    class Meta:
        ordering = ['order']

    def __str__(self):
        return f"{self.preset.title} - {self.module.name} (Step {self.order})"

class AudioEngineSettings(models.Model):
    DRIVER_CHOICES = [
        ('asio', 'ASIO'),
        ('wasapi', 'WASAPI'),
        ('directsound', 'DirectSound'),
    ]
    driver_type = models.CharField(max_length=20, choices=DRIVER_CHOICES, default='asio')
    buffer_size = models.IntegerField(default=128)
    sample_rate = models.IntegerField(default=44100)
    input_device = models.CharField(max_length=255, default='Default Input')
    output_device = models.CharField(max_length=255, default='Default Output')
    is_standalone_mode = models.BooleanField(default=True)

    def __str__(self):
        return f"Settings ({self.driver_type})"