#!/usr/bin/env python
"""
Migration ファイルをシンプルにリセットするスクリプト

使用方法:
1. 現在のmigrationテーブルの状態をバックアップ
2. migrationファイルを削除
3. 新しいシンプルなmigrationファイルを作成
4. データベースのmigration履歴をリセット
"""

import os
import shutil
from datetime import datetime

def backup_migrations():
    """現在のmigrationファイルをバックアップ"""
    migrations_dir = "rog/migrations"
    backup_dir = f"rog/migrations_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
    
    if os.path.exists(migrations_dir):
        shutil.copytree(migrations_dir, backup_dir)
        print(f"Migrationファイルを {backup_dir} にバックアップしました")
    
def reset_migrations():
    """migrationファイルをリセット"""
    migrations_dir = "rog/migrations"
    
    # __init__.py以外のファイルを削除
    if os.path.exists(migrations_dir):
        for file in os.listdir(migrations_dir):
            if file != "__init__.py" and file.endswith(".py"):
                os.remove(os.path.join(migrations_dir, file))
                print(f"削除: {file}")

def create_simple_initial_migration():
    """シンプルな初期migrationファイルを作成"""
    migration_content = '''# -*- coding: utf-8 -*-
# Generated by reset_migrations_simple.py

from django.conf import settings
from django.db import migrations, models
import django.contrib.gis.db.models.fields
import django.contrib.postgres.indexes
import django.db.models.deletion
import django.utils.timezone
import rog.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('auth', '0012_alter_user_first_name_max_length'),
    ]

    operations = [
        # 基本的なモデルのみ作成（順番に依存関係を考慮）
        
        # 1. 地理情報テーブル（managed=False）
        migrations.CreateModel(
            name='GifuAreas',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('geom', django.contrib.gis.db.models.fields.MultiPolygonField(blank=True, null=True, srid=4326)),
                ('adm0_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('area_nm', models.CharField(blank=True, max_length=254, null=True)),
            ],
            options={
                'db_table': 'gifu_areas',
                'managed': False,
            },
        ),
        
        migrations.CreateModel(
            name='JpnAdminMainPerf',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('geom', django.contrib.gis.db.models.fields.MultiPolygonField(blank=True, null=True, srid=4326)),
                ('adm0_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_pcode', models.CharField(blank=True, max_length=254, null=True)),
            ],
            options={
                'db_table': 'jpn_admin_main_perf',
                'managed': False,
            },
        ),
        
        migrations.CreateModel(
            name='JpnSubPerf',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('geom', django.contrib.gis.db.models.fields.MultiPolygonField(blank=True, null=True, srid=4326)),
                ('adm0_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm0_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm1_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_ja', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_en', models.CharField(blank=True, max_length=254, null=True)),
                ('adm2_pcode', models.CharField(blank=True, max_length=254, null=True)),
                ('name_modified', models.CharField(blank=True, max_length=254, null=True)),
                ('area_name', models.CharField(blank=True, max_length=254, null=True)),
                ('list_order', models.IntegerField(default=0)),
            ],
            options={
                'db_table': 'jpn_sub_perf',
                'managed': False,
            },
        ),
        
        # 2. ユーザー関連モデル
        migrations.CreateModel(
            name='CustomUser',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('password', models.CharField(max_length=128, verbose_name='password')),
                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
                ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
                ('email', models.EmailField(max_length=254, unique=True)),
                ('firstname', models.CharField(blank=True, max_length=255, null=True)),
                ('lastname', models.CharField(blank=True, max_length=255, null=True)),
                ('date_of_birth', models.DateField(blank=True, null=True)),
                ('female', models.BooleanField(default=False)),
                ('group', models.CharField(blank=True, max_length=255)),
                ('is_active', models.BooleanField(default=True)),
                ('is_staff', models.BooleanField(default=False)),
                ('date_joined', models.DateTimeField(default=django.utils.timezone.now)),
                ('is_rogaining', models.BooleanField(default=False)),
                ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
                ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
            ],
            options={
                'abstract': False,
            },
        ),
        
        # 3. カテゴリモデル
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('category_name', models.CharField(max_length=255, verbose_name='カテゴリ名')),
                ('parent_category', models.CharField(blank=True, max_length=255, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_updated_at', models.DateTimeField(auto_now=True)),
            ],
        ),
        
        # 4. イベント関連モデル
        migrations.CreateModel(
            name='NewEvent',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('event_code', models.CharField(max_length=255, verbose_name='イベントコード')),
                ('event_name', models.CharField(max_length=255, verbose_name='イベント名')),
                ('event_date', models.DateField(verbose_name='イベント日')),
                ('start_time', models.TimeField(blank=True, null=True, verbose_name='開始時刻')),
                ('end_time', models.TimeField(blank=True, null=True, verbose_name='終了時刻')),
                ('description', models.TextField(blank=True, null=True, verbose_name='説明')),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_updated_at', models.DateTimeField(auto_now=True)),
                ('category', models.ForeignKey(default=1, on_delete=django.db.models.deletion.DO_NOTHING, to='rog.category')),
            ],
            options={
                'db_table': 'rog_newevent',
            },
        ),
        
        # 5. チーム関連モデル
        migrations.CreateModel(
            name='Team',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('team_name', models.CharField(max_length=255, verbose_name='チーム名')),
                ('team_member_num', models.IntegerField(blank=True, default=1, null=True, verbose_name='チーム人数')),
                ('score', models.IntegerField(blank=True, default=0, null=True, verbose_name='スコア')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_updated_at', models.DateTimeField(auto_now=True)),
                ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rog.newevent')),
            ],
            options={
                'db_table': 'rog_team',
            },
        ),
        
        # 6. ロケーション関連モデル（基本的なもの）
        migrations.CreateModel(
            name='Location',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('location_id', models.IntegerField(blank=True, db_index=True, null=True, verbose_name='Location id')),
                ('sub_loc_id', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Sub location id')),
                ('cp', models.FloatField(blank=False, default=0, null=True, verbose_name='Check Point')),
                ('location_name', models.CharField(default='--- 場所をお願いします --', max_length=2048, verbose_name='Location Name')),
                ('category', models.CharField(blank=True, db_index=True, max_length=2048, null=True, verbose_name='Category')),
                ('subcategory', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Sub Category')),
                ('zip', models.CharField(blank=True, max_length=12, null=True, verbose_name='Zip code')),
                ('address', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Address')),
                ('prefecture', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Prefecture')),
                ('area', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Area')),
                ('city', models.CharField(blank=True, max_length=2048, null=True, verbose_name='City')),
                ('latitude', models.FloatField(blank=True, null=True, verbose_name='Latitude')),
                ('longitude', models.FloatField(blank=True, null=True, verbose_name='Latitude')),
                ('photos', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Photos')),
                ('videos', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Videos')),
                ('webcontents', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Web Content')),
                ('status', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Status')),
                ('portal', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Portal')),
                ('group', models.CharField(blank=True, db_index=True, max_length=2048, null=True, verbose_name='Group')),
                ('phone', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Phone')),
                ('fax', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Fax')),
                ('email', models.EmailField(blank=True, max_length=2048, null=True, verbose_name='Email')),
                ('facility', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Facility')),
                ('remark', models.CharField(blank=True, max_length=2048, null=True, verbose_name='Remarks')),
                ('tags', models.CharField(blank=True, max_length=512, null=True, verbose_name='Tags')),
                ('parammeters', models.CharField(blank=True, max_length=512, null=True, verbose_name='Parameters')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_updated_at', models.DateTimeField(auto_now=True)),
                ('last_updated_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='location_updated_user', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'rog_location',
            },
        ),
        
        # 7. エントリー関連モデル
        migrations.CreateModel(
            name='Entry',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('start_time', models.DateTimeField(blank=True, null=True, verbose_name='Start time')),
                ('goal_time', models.DateTimeField(blank=True, null=True, verbose_name='Goal time')),
                ('check_point', models.IntegerField(blank=True, null=True, verbose_name='Check Point')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_updated_at', models.DateTimeField(auto_now=True)),
                ('location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rog.location')),
                ('team', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rog.team')),
                ('last_updated_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='entry_updated_user', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'rog_entry',
            },
        ),
        
        # インデックスの追加
        migrations.AddIndex(
            model_name='gifu_areas',
            index=models.Index(fields=['geom'], name='gifu_areas_geom_idx'),
        ),
    ]
'''

    with open("rog/migrations/0001_simple_initial.py", "w", encoding="utf-8") as f:
        f.write(migration_content)
    
    print("シンプルな初期migrationファイル 0001_simple_initial.py を作成しました")

def main():
    print("=== Migration リセットスクリプト ===")
    print("1. Migrationファイルをバックアップします")
    backup_migrations()
    
    print("\\n2. 既存のmigrationファイルを削除します")
    reset_migrations()
    
    print("\\n3. シンプルな初期migrationファイルを作成します")
    create_simple_initial_migration()
    
    print("\\n=== 完了 ===")
    print("次の手順:")
    print("1. docker compose exec app python manage.py migrate --fake-initial")
    print("2. 必要に応じて追加のmigrationファイルを作成")

if __name__ == "__main__":
    main()
