"""
Migration 0006 — Update Tenant plan choices.

Old plans: starter, business, enterprise, master
New plans: starter, standard, signature, store, source

Remapping:
  business   → standard
  enterprise → signature
  master     → source
  starter    → starter  (no change)
"""
from django.db import migrations, models


def remap_plans(apps, schema_editor):
    Tenant = apps.get_model('tenants', 'Tenant')
    mapping = {
        'business':   'standard',
        'enterprise': 'signature',
        'master':     'source',
    }
    for old, new in mapping.items():
        Tenant.objects.filter(plan=old).update(plan=new)


def reverse_remap_plans(apps, schema_editor):
    Tenant = apps.get_model('tenants', 'Tenant')
    mapping = {
        'standard':  'business',
        'signature': 'enterprise',
        'source':    'master',
    }
    for old, new in mapping.items():
        Tenant.objects.filter(plan=old).update(plan=new)


class Migration(migrations.Migration):

    dependencies = [
        ('tenants', '0005_tenant_chat_colors'),
    ]

    operations = [
        migrations.AlterField(
            model_name='tenant',
            name='plan',
            field=models.CharField(
                choices=[
                    ('starter',   'Starter'),
                    ('standard',  'Standard'),
                    ('signature', 'Signature'),
                    ('store',     'Store'),
                    ('source',    'Source'),
                ],
                default='starter',
                max_length=20,
            ),
        ),
        migrations.RunPython(remap_plans, reverse_remap_plans),
    ]
