# Generated migration for Media Management System

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', '0006_add_article_like'),
    ]

    operations = [
        # 创建 MediaFile 模型
        migrations.CreateModel(
            name='MediaFile',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('original_filename', models.CharField(max_length=255, verbose_name='original filename')),
                ('stored_filename', models.CharField(max_length=255, unique=True, verbose_name='stored filename')),
                ('file_type', models.CharField(choices=[('image', 'Image'), ('file', 'File')], max_length=10, verbose_name='file type')),
                ('file_size', models.BigIntegerField(verbose_name='file size')),
                ('file_hash', models.CharField(db_index=True, max_length=32, verbose_name='file hash')),
                ('mime_type', models.CharField(max_length=100, verbose_name='MIME type')),
                ('file_path', models.CharField(max_length=500, verbose_name='file path')),
                ('upload_time', models.DateTimeField(db_index=True, default=django.utils.timezone.now, verbose_name='upload time')),
                ('width', models.IntegerField(blank=True, null=True, verbose_name='width')),
                ('height', models.IntegerField(blank=True, null=True, verbose_name='height')),
                ('thumbnail_path', models.CharField(blank=True, max_length=500, verbose_name='thumbnail path')),
                ('description', models.TextField(blank=True, verbose_name='description')),
                ('is_public', models.BooleanField(default=True, verbose_name='is public')),
                ('reference_count', models.IntegerField(default=0, verbose_name='reference count')),
                ('uploader', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='uploaded_files', to=settings.AUTH_USER_MODEL, verbose_name='uploader')),
            ],
            options={
                'verbose_name': 'media file',
                'verbose_name_plural': 'media files',
                'ordering': ['-upload_time'],
            },
        ),

        # 创建 MediaFolder 模型
        migrations.CreateModel(
            name='MediaFolder',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=100, verbose_name='folder name')),
                ('created_time', models.DateTimeField(default=django.utils.timezone.now, verbose_name='created time')),
                ('description', models.TextField(blank=True, verbose_name='description')),
                ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='media_folders', to=settings.AUTH_USER_MODEL, verbose_name='owner')),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subfolders', to='blog.mediafolder', verbose_name='parent folder')),
            ],
            options={
                'verbose_name': 'media folder',
                'verbose_name_plural': 'media folders',
                'ordering': ['name'],
            },
        ),

        # 创建 MediaFileFolder 模型
        migrations.CreateModel(
            name='MediaFileFolder',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('added_time', models.DateTimeField(default=django.utils.timezone.now, verbose_name='added time')),
                ('file', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='folder_relations', to='blog.mediafile')),
                ('folder', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='file_relations', to='blog.mediafolder')),
            ],
            options={
                'verbose_name': 'media file folder relation',
                'verbose_name_plural': 'media file folder relations',
            },
        ),

        # 添加索引
        migrations.AddIndex(
            model_name='mediafile',
            index=models.Index(fields=['file_type', '-upload_time'], name='media_type_time_idx'),
        ),
        migrations.AddIndex(
            model_name='mediafile',
            index=models.Index(fields=['uploader', '-upload_time'], name='media_uploader_time_idx'),
        ),
        migrations.AddIndex(
            model_name='mediafile',
            index=models.Index(fields=['file_hash'], name='media_hash_idx'),
        ),

        # 添加唯一约束
        migrations.AlterUniqueTogether(
            name='mediafolder',
            unique_together={('name', 'parent', 'owner')},
        ),
        migrations.AlterUniqueTogether(
            name='mediafilefolder',
            unique_together={('file', 'folder')},
        ),
    ]
