# 用户关注和收藏功能 - 数据库迁移
# Generated manually for social features

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


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('blog', '0004_add_article_draft'),
    ]

    operations = [
        # 创建用户关注模型
        migrations.CreateModel(
            name='UserFollow',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('creation_time', models.DateTimeField(db_index=True, default=django.utils.timezone.now, verbose_name='creation time')),
                ('follower', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='following', to=settings.AUTH_USER_MODEL, verbose_name='follower')),
                ('following', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='followers', to=settings.AUTH_USER_MODEL, verbose_name='following')),
            ],
            options={
                'verbose_name': 'user follow',
                'verbose_name_plural': 'user follows',
                'ordering': ['-creation_time'],
            },
        ),

        # 创建文章收藏模型
        migrations.CreateModel(
            name='ArticleFavorite',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('creation_time', models.DateTimeField(db_index=True, default=django.utils.timezone.now, verbose_name='creation time')),
                ('note', models.CharField(blank=True, default='', max_length=200, verbose_name='note')),
                ('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='favorited_by', to='blog.Article', verbose_name='article')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='favorites', to=settings.AUTH_USER_MODEL, verbose_name='user')),
            ],
            options={
                'verbose_name': 'article favorite',
                'verbose_name_plural': 'article favorites',
                'ordering': ['-creation_time'],
            },
        ),

        # 添加索引
        migrations.AddIndex(
            model_name='userfollow',
            index=models.Index(fields=['follower', '-creation_time'], name='follow_follower_time_idx'),
        ),
        migrations.AddIndex(
            model_name='userfollow',
            index=models.Index(fields=['following', '-creation_time'], name='follow_following_time_idx'),
        ),
        migrations.AddIndex(
            model_name='articlefavorite',
            index=models.Index(fields=['user', '-creation_time'], name='favorite_user_time_idx'),
        ),
        migrations.AddIndex(
            model_name='articlefavorite',
            index=models.Index(fields=['article', '-creation_time'], name='favorite_article_time_idx'),
        ),

        # 添加唯一约束
        migrations.AlterUniqueTogether(
            name='userfollow',
            unique_together={('follower', 'following')},
        ),
        migrations.AlterUniqueTogether(
            name='articlefavorite',
            unique_together={('user', 'article')},
        ),
    ]
