# Generated for safe deployment
# Conditional SQL operations to prevent duplicate table errors

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('rog', '0009_create_gps_tables_safe'),
    ]

    operations = [
        # 既存のすべてのテーブルとインデックスを条件付きで作成
        migrations.RunSQL(
            sql="""
            -- CustomUser テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_customuser') THEN
                    CREATE TABLE rog_customuser (
                        id SERIAL PRIMARY KEY,
                        password VARCHAR(128) NOT NULL,
                        last_login TIMESTAMP WITH TIME ZONE,
                        is_superuser BOOLEAN NOT NULL,
                        username VARCHAR(150) NOT NULL UNIQUE,
                        first_name VARCHAR(150) NOT NULL,
                        last_name VARCHAR(150) NOT NULL,
                        email VARCHAR(254) NOT NULL,
                        is_staff BOOLEAN NOT NULL,
                        is_active BOOLEAN NOT NULL,
                        date_joined TIMESTAMP WITH TIME ZONE NOT NULL
                    );
                END IF;
            END $$;
            
            -- CustomUser インデックスの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_customuser' AND indexname = 'rog_customuser_username_key') THEN
                    CREATE UNIQUE INDEX rog_customuser_username_key ON rog_customuser (username);
                END IF;
            END $$;
            
            -- Entry テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_entry') THEN
                    CREATE TABLE rog_entry (
                        id SERIAL PRIMARY KEY,
                        event_code VARCHAR(50),
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );
                END IF;
            END $$;
            
            -- Team テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_team') THEN
                    CREATE TABLE rog_team (
                        id SERIAL PRIMARY KEY,
                        team_name VARCHAR(100),
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );
                END IF;
            END $$;
            
            -- Member テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_member') THEN
                    CREATE TABLE rog_member (
                        id SERIAL PRIMARY KEY,
                        member_name VARCHAR(100),
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );
                END IF;
            END $$;
            
            -- Location2025 テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_location2025') THEN
                    CREATE TABLE rog_location2025 (
                        id SERIAL PRIMARY KEY,
                        cp_number INTEGER,
                        cp_name VARCHAR(255),
                        latitude FLOAT,
                        longitude FLOAT,
                        location GEOMETRY(POINT, 4326),
                        cp_point INTEGER DEFAULT 10,
                        photo_point INTEGER DEFAULT 0,
                        buy_point INTEGER DEFAULT 0,
                        checkin_radius FLOAT DEFAULT 15.0,
                        auto_checkin BOOLEAN DEFAULT FALSE,
                        shop_closed BOOLEAN DEFAULT FALSE,
                        shop_shutdown BOOLEAN DEFAULT FALSE,
                        opening_hours TEXT,
                        address VARCHAR(512),
                        phone VARCHAR(32),
                        website VARCHAR(200),
                        description TEXT,
                        is_active BOOLEAN DEFAULT TRUE,
                        sort_order INTEGER DEFAULT 0,
                        csv_source_file VARCHAR(255),
                        csv_upload_date TIMESTAMP WITH TIME ZONE,
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        event_id INTEGER,
                        created_by_id INTEGER,
                        csv_upload_user_id INTEGER,
                        updated_by_id INTEGER
                    );
                END IF;
            END $$;
            
            -- GPS Checkin テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_gpscheckin') THEN
                    CREATE TABLE rog_gpscheckin (
                        id SERIAL PRIMARY KEY,
                        event_code VARCHAR(50),
                        zekken VARCHAR(20),
                        serial_number VARCHAR(50),
                        team_name VARCHAR(100),
                        cp_number VARCHAR(20),
                        lat FLOAT,
                        lng FLOAT,
                        checkin_time TIMESTAMP WITH TIME ZONE,
                        record_time TIMESTAMP WITH TIME ZONE,
                        goal_time TIMESTAMP WITH TIME ZONE,
                        late_point INTEGER,
                        buy_flag BOOLEAN DEFAULT FALSE,
                        image_address TEXT,
                        minus_photo_flag BOOLEAN DEFAULT FALSE,
                        create_user TEXT,
                        update_user TEXT,
                        comment TEXT,
                        location GEOMETRY(POINT, 4326),
                        mobserver_id INTEGER,
                        event_id INTEGER,
                        team_id INTEGER,
                        checkpoint_id INTEGER
                    );
                END IF;
            END $$;
            
            -- GPS Log テーブルの安全な作成
            DO $$ 
            BEGIN
                IF NOT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rog_gpslog') THEN
                    CREATE TABLE rog_gpslog (
                        id SERIAL PRIMARY KEY,
                        event_code VARCHAR(50),
                        zekken VARCHAR(20),
                        serial_number VARCHAR(50),
                        cp_number VARCHAR(20),
                        lat FLOAT,
                        lng FLOAT,
                        checkin_time TIMESTAMP WITH TIME ZONE,
                        record_time TIMESTAMP WITH TIME ZONE,
                        location GEOMETRY(POINT, 4326),
                        mobserver_id INTEGER,
                        event_id INTEGER,
                        team_id INTEGER,
                        checkpoint_id INTEGER
                    );
                END IF;
            END $$;
            
            -- 必要なインデックスの安全な作成
            DO $$ 
            BEGIN
                -- GPS Checkin インデックス
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_gpscheckin' AND indexname = 'rog_gpscheckin_event_code_idx') THEN
                    CREATE INDEX rog_gpscheckin_event_code_idx ON rog_gpscheckin (event_code);
                END IF;
                
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_gpscheckin' AND indexname = 'rog_gpscheckin_serial_number_idx') THEN
                    CREATE INDEX rog_gpscheckin_serial_number_idx ON rog_gpscheckin (serial_number);
                END IF;
                
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_gpscheckin' AND indexname = 'rog_gpscheckin_team_name_idx') THEN
                    CREATE INDEX rog_gpscheckin_team_name_idx ON rog_gpscheckin (team_name);
                END IF;
                
                -- GPS Log インデックス
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_gpslog' AND indexname = 'rog_gpslog_event_code_idx') THEN
                    CREATE INDEX rog_gpslog_event_code_idx ON rog_gpslog (event_code);
                END IF;
                
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_gpslog' AND indexname = 'rog_gpslog_serial_number_idx') THEN
                    CREATE INDEX rog_gpslog_serial_number_idx ON rog_gpslog (serial_number);
                END IF;
                
                -- Location2025 インデックス
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_location2025' AND indexname = 'rog_location2025_cp_number_idx') THEN
                    CREATE INDEX rog_location2025_cp_number_idx ON rog_location2025 (cp_number);
                END IF;
                
                IF NOT EXISTS (SELECT FROM pg_indexes WHERE tablename = 'rog_location2025' AND indexname = 'rog_location2025_event_id_idx') THEN
                    CREATE INDEX rog_location2025_event_id_idx ON rog_location2025 (event_id);
                END IF;
            END $$;
            """,
            reverse_sql="""
            -- 逆操作は何もしない（テーブルを削除しない）
            SELECT 1;
            """
        ),
    ]
