# Generated by Django 3.2.9 on 2025-08-27 05:59

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 = [
        ('rog', '0010_safe_create_all_tables'),
    ]

    operations = [
        migrations.CreateModel(
            name='AppVersion',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('version', models.CharField(help_text='セマンティックバージョン (1.2.3)', max_length=20)),
                ('platform', models.CharField(choices=[('android', 'Android'), ('ios', 'iOS')], max_length=10)),
                ('build_number', models.CharField(blank=True, max_length=20, null=True)),
                ('is_latest', models.BooleanField(default=False, help_text='最新版フラグ')),
                ('is_required', models.BooleanField(default=False, help_text='強制更新フラグ')),
                ('update_message', models.TextField(blank=True, help_text='ユーザー向け更新メッセージ', null=True)),
                ('download_url', models.URLField(blank=True, help_text='アプリストアURL', null=True)),
                ('release_date', models.DateTimeField(default=django.utils.timezone.now)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
            ],
            options={
                'db_table': 'app_versions',
            },
        ),
        migrations.CreateModel(
            name='CheckinExtended',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('gps_latitude', models.DecimalField(blank=True, decimal_places=8, max_digits=10, null=True)),
                ('gps_longitude', models.DecimalField(blank=True, decimal_places=8, max_digits=11, null=True)),
                ('gps_accuracy', models.DecimalField(blank=True, decimal_places=2, help_text='GPS精度（メートル）', max_digits=6, null=True)),
                ('gps_timestamp', models.DateTimeField(blank=True, null=True)),
                ('camera_capture_time', models.DateTimeField(blank=True, null=True)),
                ('device_info', models.TextField(blank=True, null=True)),
                ('validation_status', models.CharField(choices=[('pending', 'Pending'), ('approved', 'Approved'), ('rejected', 'Rejected'), ('requires_review', 'Requires Review')], default='pending', max_length=20)),
                ('validation_comment', models.TextField(blank=True, null=True)),
                ('validated_at', models.DateTimeField(blank=True, null=True)),
                ('bonus_points', models.IntegerField(default=0)),
                ('scoring_breakdown', models.JSONField(blank=True, default=dict)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'db_table': 'rog_checkin_extended',
            },
        ),
        migrations.CreateModel(
            name='UploadedImage',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('original_filename', models.CharField(max_length=255)),
                ('server_filename', models.CharField(max_length=255, unique=True)),
                ('file_url', models.URLField()),
                ('file_size', models.BigIntegerField()),
                ('mime_type', models.CharField(choices=[('image/jpeg', 'JPEG'), ('image/png', 'PNG'), ('image/heic', 'HEIC'), ('image/webp', 'WebP')], max_length=50)),
                ('event_code', models.CharField(blank=True, max_length=50, null=True)),
                ('team_name', models.CharField(blank=True, max_length=255, null=True)),
                ('cp_number', models.IntegerField(blank=True, null=True)),
                ('upload_source', models.CharField(choices=[('direct', 'Direct'), ('sharing_intent', 'Sharing Intent'), ('bulk_upload', 'Bulk Upload')], default='direct', max_length=50)),
                ('device_platform', models.CharField(blank=True, choices=[('ios', 'iOS'), ('android', 'Android'), ('web', 'Web')], max_length=20, null=True)),
                ('capture_timestamp', models.DateTimeField(blank=True, null=True)),
                ('upload_timestamp', models.DateTimeField(auto_now_add=True)),
                ('device_info', models.TextField(blank=True, null=True)),
                ('processing_status', models.CharField(choices=[('uploaded', 'Uploaded'), ('processing', 'Processing'), ('processed', 'Processed'), ('failed', 'Failed')], default='uploaded', max_length=20)),
                ('thumbnail_url', models.URLField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'db_table': 'rog_uploaded_images',
            },
        ),
        # TeamStartテーブルのentryカラムを安全に削除
        migrations.RunSQL(
            sql="""
            DO $$ 
            BEGIN
                IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_teamstart') THEN
                    IF EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_teamstart' AND column_name = 'entry_id') THEN
                        ALTER TABLE rog_teamstart DROP COLUMN entry_id;
                    END IF;
                END IF;
            END $$;
            """,
            reverse_sql="-- No reverse SQL needed for conditional operation"
        ),
        # Entryテーブルに新しいフィールドを条件付きで追加
        migrations.RunSQL(
            sql="""
            DO $$ 
            BEGIN
                -- can_access_private_events フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_entry' AND column_name = 'can_access_private_events') THEN
                    ALTER TABLE rog_entry ADD COLUMN can_access_private_events BOOLEAN DEFAULT FALSE;
                    COMMENT ON COLUMN rog_entry.can_access_private_events IS '非公開イベント参加権限';
                END IF;
                
                -- staff_privileges フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_entry' AND column_name = 'staff_privileges') THEN
                    ALTER TABLE rog_entry ADD COLUMN staff_privileges BOOLEAN DEFAULT FALSE;
                    COMMENT ON COLUMN rog_entry.staff_privileges IS 'スタッフ権限フラグ';
                END IF;
                
                -- team_validation_status フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_entry' AND column_name = 'team_validation_status') THEN
                    ALTER TABLE rog_entry ADD COLUMN team_validation_status VARCHAR(20) DEFAULT 'approved';
                    COMMENT ON COLUMN rog_entry.team_validation_status IS 'チーム承認状況';
                END IF;
                
                -- zekken_label フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_entry' AND column_name = 'zekken_label') THEN
                    ALTER TABLE rog_entry ADD COLUMN zekken_label VARCHAR(255);
                END IF;
            END $$;
            """,
            reverse_sql="-- No reverse SQL needed for conditional operation"
        ),
        # GPSCheckinテーブルに新しいフィールドを条件付きで追加
        migrations.RunSQL(
            sql="""
            DO $$ 
            BEGIN
                -- create_at フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_gpscheckin' AND column_name = 'create_at') THEN
                    ALTER TABLE rog_gpscheckin ADD COLUMN create_at TIMESTAMP WITH TIME ZONE;
                    COMMENT ON COLUMN rog_gpscheckin.create_at IS '作成日時：データの作成日時';
                END IF;
                
                -- create_user フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_gpscheckin' AND column_name = 'create_user') THEN
                    ALTER TABLE rog_gpscheckin ADD COLUMN create_user TEXT;
                    COMMENT ON COLUMN rog_gpscheckin.create_user IS '作成ユーザー';
                END IF;
                
                -- event_id フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_gpscheckin' AND column_name = 'event_id') THEN
                    ALTER TABLE rog_gpscheckin ADD COLUMN event_id INTEGER;
                    COMMENT ON COLUMN rog_gpscheckin.event_id IS 'イベントID';
                END IF;
                
                -- goal_time フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_gpscheckin' AND column_name = 'goal_time') THEN
                    ALTER TABLE rog_gpscheckin ADD COLUMN goal_time TEXT;
                    COMMENT ON COLUMN rog_gpscheckin.goal_time IS 'ゴール時刻=ゴール時のみ使用される。画像から時刻を読み取り設定する。';
                END IF;
                
                -- image_address フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_gpscheckin' AND column_name = 'image_address') THEN
                    ALTER TABLE rog_gpscheckin ADD COLUMN image_address TEXT;
                    COMMENT ON COLUMN rog_gpscheckin.image_address IS 'チェックイン画像のパス';
                END IF;
            END $$;
            """,
            reverse_sql="-- No reverse SQL needed for conditional operation"
        ),
        # NewEvent2テーブルに新しいフィールドを条件付きで追加
        migrations.RunSQL(
            sql="""
            DO $$ 
            BEGIN
                -- status フィールドの追加
                IF NOT EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'rog_newevent2' AND column_name = 'status') THEN
                    ALTER TABLE rog_newevent2 ADD COLUMN status VARCHAR(20) DEFAULT 'draft';
                    COMMENT ON COLUMN rog_newevent2.status IS 'イベントステータス';
                END IF;
            END $$;
            """,
            reverse_sql="-- No reverse SQL needed for conditional operation"
        ),
        migrations.AlterModelTable(
            name='gpslog',
            table='gps_information',
        ),
        migrations.DeleteModel(
            name='TeamGoal',
        ),
        migrations.DeleteModel(
            name='TeamStart',
        ),
        migrations.AddField(
            model_name='uploadedimage',
            name='entry',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rog.entry'),
        ),
        migrations.AddField(
            model_name='uploadedimage',
            name='gpslog',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rog.gpscheckin'),
        ),
        migrations.AddField(
            model_name='checkinextended',
            name='gpslog',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='extended_info', to='rog.gpscheckin'),
        ),
        migrations.AddField(
            model_name='checkinextended',
            name='validated_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddIndex(
            model_name='appversion',
            index=models.Index(fields=['platform'], name='idx_app_versions_platform'),
        ),
        migrations.AddIndex(
            model_name='appversion',
            index=models.Index(condition=models.Q(('is_latest', True)), fields=['is_latest'], name='idx_app_versions_latest_true'),
        ),
        migrations.AlterUniqueTogether(
            name='appversion',
            unique_together={('version', 'platform')},
        ),
        migrations.AddIndex(
            model_name='uploadedimage',
            index=models.Index(fields=['event_code', 'team_name'], name='idx_uploaded_event_team'),
        ),
        migrations.AddIndex(
            model_name='uploadedimage',
            index=models.Index(fields=['cp_number'], name='idx_uploaded_cp_number'),
        ),
        migrations.AddIndex(
            model_name='uploadedimage',
            index=models.Index(fields=['upload_timestamp'], name='idx_uploaded_timestamp'),
        ),
        migrations.AddIndex(
            model_name='uploadedimage',
            index=models.Index(fields=['processing_status'], name='idx_uploaded_status'),
        ),
        migrations.AddIndex(
            model_name='checkinextended',
            index=models.Index(fields=['validation_status'], name='idx_checkin_ext_valid'),
        ),
        migrations.AddIndex(
            model_name='checkinextended',
            index=models.Index(fields=['created_at'], name='idx_checkin_ext_created'),
        ),
    ]
