# Part 3 — configurable pricing, duration, subscriptions (NOT APPLIED YET)
# Review, then: python manage.py migrate courses 0007

import django.core.validators
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('courses', '0006_projects_and_module_type'),
    ]

    operations = [
        migrations.AddField(
            model_name='course',
            name='bundle_discount_percent',
            field=models.PositiveSmallIntegerField(default=0, help_text='Discount %% when buying learning_price + exam_fee together.', validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(100)]),
        ),
        migrations.AddField(
            model_name='course',
            name='currency',
            field=models.CharField(default='PKR', help_text='ISO currency code for this course (default PKR; USD later for international).', max_length=3),
        ),
        migrations.AddField(
            model_name='course',
            name='duration_weeks',
            field=models.PositiveIntegerField(default=8, help_text='Estimated self-paced or mentor-paced completion time in weeks.'),
        ),
        migrations.AddField(
            model_name='course',
            name='estimated_completion_months',
            field=models.PositiveSmallIntegerField(default=3, help_text='Shown so candidates can estimate total subscription cost.'),
        ),
        migrations.AddField(
            model_name='course',
            name='mentorship_price',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Optional mentor add-on price. Null if mentorship not offered.', max_digits=10, null=True),
        ),
        migrations.AddField(
            model_name='course',
            name='min_subscription_months',
            field=models.PositiveSmallIntegerField(default=1, help_text='Minimum commitment in months (e.g. 1).'),
        ),
        migrations.AddField(
            model_name='course',
            name='subscription_enabled',
            field=models.BooleanField(default=False, help_text='Offer monthly pay-as-you-go for Learning Path.'),
        ),
        migrations.AddField(
            model_name='course',
            name='subscription_price_monthly',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Monthly subscription price (PKR power-adjusted, independent of one-time fees).', max_digits=10, null=True),
        ),
        migrations.AlterField(
            model_name='course',
            name='direct_exam_only_price',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Direct Certification Path price if different from exam_fee.', max_digits=10, null=True),
        ),
        migrations.AlterField(
            model_name='course',
            name='exam_fee',
            field=models.DecimalField(decimal_places=2, default=0, help_text='Certification exam fee (both Learning and Direct paths).', max_digits=10),
        ),
        migrations.AlterField(
            model_name='course',
            name='learning_price',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Learning Path access (modules + projects). Null if no learning content.', max_digits=10, null=True),
        ),
        migrations.AlterField(
            model_name='course',
            name='mentorship_available',
            field=models.BooleanField(default=False, help_text='Learning Path only: optional paid mentorship add-on can be offered.'),
        ),
        migrations.AlterField(
            model_name='course',
            name='price',
            field=models.DecimalField(decimal_places=2, default=0.0, help_text='Legacy course card price. Prefer learning_price / exam fees below.', max_digits=10),
        ),
        migrations.CreateModel(
            name='CoursePricingHistory',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('field_changed', models.CharField(max_length=64)),
                ('old_price', models.CharField(blank=True, help_text='Previous value as string (supports decimals and currency codes).', max_length=64)),
                ('new_price', models.CharField(blank=True, max_length=64)),
                ('changed_at', models.DateTimeField(auto_now_add=True)),
                ('note', models.CharField(blank=True, max_length=255)),
                ('changed_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='course_price_changes', to=settings.AUTH_USER_MODEL)),
                ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pricing_history', to='courses.course')),
            ],
            options={
                'verbose_name': 'Course pricing history',
                'verbose_name_plural': 'Course pricing history',
                'ordering': ['-changed_at'],
            },
        ),
        migrations.CreateModel(
            name='LearningSubscription',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('status', models.CharField(choices=[('active', 'Active'), ('paused', 'Paused'), ('cancelled', 'Cancelled'), ('completed', 'Completed')], db_index=True, default='active', max_length=20)),
                ('started_at', models.DateTimeField(auto_now_add=True)),
                ('current_period_end', models.DateTimeField(help_text='End of the paid month; candidate renews before/at this date.')),
                ('months_paid', models.PositiveIntegerField(default=1)),
                ('monthly_price_locked', models.DecimalField(decimal_places=2, help_text='Price locked at subscribe time (honors old rates if fees rise).', max_digits=10)),
                ('currency_locked', models.CharField(default='PKR', max_length=3)),
                ('cancelled_at', models.DateTimeField(blank=True, null=True)),
                ('renew_reminder_sent_at', models.DateTimeField(blank=True, help_text='Last manual-renew reminder (email/WhatsApp later).', null=True)),
                ('notes', models.TextField(blank=True)),
                ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='subscriptions', to='courses.course')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='learning_subscriptions', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Learning subscription',
                'verbose_name_plural': 'Learning subscriptions',
                'ordering': ['-started_at'],
            },
        ),
        migrations.AddConstraint(
            model_name='learningsubscription',
            constraint=models.UniqueConstraint(condition=models.Q(status='active'), fields=('user', 'course'), name='uniq_active_learning_subscription'),
        ),
    ]
