# Generated by Django 5.2.8 on 2025-11-29 02:17

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('schedule', '0014_whitelistedurl'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='scheduledtask',
            name='plugin_code',
            field=models.TextField(blank=True, help_text='Custom plugin code (for custom scripts)'),
        ),
        migrations.CreateModel(
            name='PluginTemplate',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(help_text="Plugin name (e.g., 'Win Back Inactive Customers')", max_length=200)),
                ('slug', models.SlugField(help_text='URL-friendly identifier', max_length=200, unique=True)),
                ('description', models.TextField(help_text='What this plugin does (markdown supported)')),
                ('short_description', models.CharField(help_text='One-line summary for marketplace listing', max_length=200)),
                ('plugin_code', models.TextField(help_text='The Python script code')),
                ('plugin_code_hash', models.CharField(blank=True, help_text='SHA-256 hash of code for verification', max_length=64)),
                ('template_variables', models.JSONField(blank=True, default=dict, help_text='Template variables extracted from code (PROMPT, CONTEXT, DATE)')),
                ('default_config', models.JSONField(blank=True, default=dict, help_text='Default configuration values')),
                ('visibility', models.CharField(choices=[('PRIVATE', 'Private (only me)'), ('PUBLIC', 'Public (marketplace)'), ('PLATFORM', 'Platform Official')], db_index=True, default='PRIVATE', max_length=10)),
                ('category', models.CharField(choices=[('EMAIL', 'Email & Notifications'), ('REPORTS', 'Reports & Analytics'), ('CUSTOMER', 'Customer Engagement'), ('BOOKING', 'Booking & Scheduling'), ('INTEGRATION', 'Third-party Integration'), ('AUTOMATION', 'General Automation'), ('OTHER', 'Other')], db_index=True, default='OTHER', max_length=20)),
                ('tags', models.JSONField(blank=True, default=list, help_text="Searchable tags (e.g., ['email', 'customers', 'retention'])")),
                ('author_name', models.CharField(blank=True, help_text='Display name for attribution', max_length=200)),
                ('license_type', models.CharField(default='SCPL', help_text='SCPL for marketplace, or custom for private', max_length=10)),
                ('is_approved', models.BooleanField(db_index=True, default=False, help_text='Approved for marketplace by platform staff')),
                ('approved_at', models.DateTimeField(blank=True, null=True)),
                ('rejection_reason', models.TextField(blank=True, help_text='Reason for rejection if not approved')),
                ('install_count', models.PositiveIntegerField(default=0, help_text='Number of times installed')),
                ('rating_average', models.DecimalField(decimal_places=2, default=0.0, help_text='Average rating (0-5)', max_digits=3)),
                ('rating_count', models.PositiveIntegerField(default=0, help_text='Number of ratings')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('published_at', models.DateTimeField(blank=True, help_text='When published to marketplace', null=True)),
                ('approved_by', models.ForeignKey(blank=True, help_text='Platform user who approved this plugin', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='approved_plugins', to=settings.AUTH_USER_MODEL)),
                ('author', models.ForeignKey(help_text='Original author', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='plugin_templates', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PluginInstallation',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('installed_at', models.DateTimeField(auto_now_add=True)),
                ('config_values', models.JSONField(default=dict, help_text="User's configuration values at installation")),
                ('template_version_hash', models.CharField(blank=True, help_text='Hash of template code at install time (for update detection)', max_length=64)),
                ('rating', models.PositiveSmallIntegerField(blank=True, help_text='User rating (1-5 stars)', null=True)),
                ('review', models.TextField(blank=True, help_text='User review/feedback')),
                ('reviewed_at', models.DateTimeField(blank=True, null=True)),
                ('installed_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='plugin_installations', to=settings.AUTH_USER_MODEL)),
                ('scheduled_task', models.OneToOneField(help_text='The created scheduled task', on_delete=django.db.models.deletion.CASCADE, related_name='installation', to='schedule.scheduledtask')),
                ('template', models.ForeignKey(help_text='Source template (null if template deleted)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='installations', to='schedule.plugintemplate')),
            ],
            options={
                'ordering': ['-installed_at'],
            },
        ),
        migrations.AddIndex(
            model_name='plugintemplate',
            index=models.Index(fields=['visibility', 'is_approved', '-install_count'], name='schedule_pl_visibil_5a0373_idx'),
        ),
        migrations.AddIndex(
            model_name='plugintemplate',
            index=models.Index(fields=['category', 'visibility'], name='schedule_pl_categor_059ef0_idx'),
        ),
        migrations.AddIndex(
            model_name='plugintemplate',
            index=models.Index(fields=['slug'], name='schedule_pl_slug_f788ca_idx'),
        ),
        migrations.AddIndex(
            model_name='plugininstallation',
            index=models.Index(fields=['template', '-installed_at'], name='schedule_pl_templat_9cc5b3_idx'),
        ),
        migrations.AddIndex(
            model_name='plugininstallation',
            index=models.Index(fields=['installed_by', '-installed_at'], name='schedule_pl_install_5b0143_idx'),
        ),
    ]
