-- rog_location2025テーブル手動作成SQL (デプロイ先用)
-- 実行前に必要な拡張機能が有効になっていることを確認してください
-- CREATE EXTENSION IF NOT EXISTS postgis;

-- 既存テーブルが存在する場合は削除 (必要に応じてコメントアウト)
-- DROP TABLE IF EXISTS rog_location2025;

-- rog_location2025テーブル作成
CREATE TABLE IF NOT EXISTS rog_location2025 (
    id BIGSERIAL PRIMARY KEY,
    cp_number INTEGER NOT NULL,
    event_id INTEGER NOT NULL,
    cp_name VARCHAR(255) NOT NULL,
    latitude DOUBLE PRECISION,
    longitude DOUBLE PRECISION,
    location GEOMETRY(POINT, 4326),
    cp_point INTEGER NOT NULL DEFAULT 10,
    photo_point INTEGER NOT NULL DEFAULT 0,
    buy_point INTEGER NOT NULL DEFAULT 0,
    checkin_radius DOUBLE PRECISION NOT NULL DEFAULT 15.0,
    auto_checkin BOOLEAN NOT NULL DEFAULT false,
    shop_closed BOOLEAN NOT NULL DEFAULT false,
    shop_shutdown BOOLEAN NOT NULL DEFAULT false,
    opening_hours TEXT,
    address VARCHAR(512),
    phone VARCHAR(32),
    website VARCHAR(200),
    description TEXT,
    is_active BOOLEAN NOT NULL DEFAULT true,
    sort_order INTEGER NOT NULL DEFAULT 0,
    csv_source_file VARCHAR(255),
    csv_upload_date TIMESTAMP WITH TIME ZONE,
    csv_upload_user_id BIGINT,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_by_id BIGINT,
    updated_by_id BIGINT
);

-- インデックス作成
CREATE INDEX IF NOT EXISTS rog_location2025_cp_number_idx ON rog_location2025 (cp_number);
CREATE INDEX IF NOT EXISTS rog_location2025_event_id_idx ON rog_location2025 (event_id);
CREATE INDEX IF NOT EXISTS rog_location2025_is_active_idx ON rog_location2025 (is_active);
CREATE INDEX IF NOT EXISTS location2025_event_cp_idx ON rog_location2025 (event_id, cp_number);
CREATE INDEX IF NOT EXISTS location2025_event_active_idx ON rog_location2025 (event_id, is_active);
CREATE INDEX IF NOT EXISTS location2025_csv_date_idx ON rog_location2025 (csv_upload_date);

-- 空間インデックス (PostGIS必須)
CREATE INDEX IF NOT EXISTS location2025_location_gist_idx ON rog_location2025 USING GIST (location);

-- 外部キー制約追加 (テーブルが存在する場合)
-- rog_newevent2テーブルが存在することを前提
DO $$ 
BEGIN
    -- event_idの外部キー制約
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'rog_newevent2') THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_name = 'rog_location2025_event_id_fkey'
        ) THEN
            ALTER TABLE rog_location2025 
            ADD CONSTRAINT rog_location2025_event_id_fkey 
            FOREIGN KEY (event_id) REFERENCES rog_newevent2(id) DEFERRABLE INITIALLY DEFERRED;
        END IF;
    END IF;
    
    -- csv_upload_user_idの外部キー制約
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'rog_customuser') THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_name = 'rog_location2025_csv_upload_user_id_fkey'
        ) THEN
            ALTER TABLE rog_location2025 
            ADD CONSTRAINT rog_location2025_csv_upload_user_id_fkey 
            FOREIGN KEY (csv_upload_user_id) REFERENCES rog_customuser(id) DEFERRABLE INITIALLY DEFERRED;
        END IF;
    END IF;
    
    -- created_by_idの外部キー制約
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'rog_customuser') THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_name = 'rog_location2025_created_by_id_fkey'
        ) THEN
            ALTER TABLE rog_location2025 
            ADD CONSTRAINT rog_location2025_created_by_id_fkey 
            FOREIGN KEY (created_by_id) REFERENCES rog_customuser(id) DEFERRABLE INITIALLY DEFERRED;
        END IF;
    END IF;
    
    -- updated_by_idの外部キー制約
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'rog_customuser') THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_name = 'rog_location2025_updated_by_id_fkey'
        ) THEN
            ALTER TABLE rog_location2025 
            ADD CONSTRAINT rog_location2025_updated_by_id_fkey 
            FOREIGN KEY (updated_by_id) REFERENCES rog_customuser(id) DEFERRABLE INITIALLY DEFERRED;
        END IF;
    END IF;
    
    -- ユニーク制約
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints 
        WHERE constraint_name = 'rog_location2025_cp_number_event_id_unique'
    ) THEN
        ALTER TABLE rog_location2025 
        ADD CONSTRAINT rog_location2025_cp_number_event_id_unique 
        UNIQUE (cp_number, event_id);
    END IF;
END $$;

-- updated_atの自動更新トリガー作成
CREATE OR REPLACE FUNCTION update_rog_location2025_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS rog_location2025_updated_at_trigger ON rog_location2025;
CREATE TRIGGER rog_location2025_updated_at_trigger
    BEFORE UPDATE ON rog_location2025
    FOR EACH ROW
    EXECUTE FUNCTION update_rog_location2025_updated_at();

-- 作成確認
SELECT 
    schemaname,
    tablename,
    tableowner
FROM pg_tables 
WHERE tablename = 'rog_location2025';

-- カラム確認
SELECT 
    column_name,
    data_type,
    is_nullable,
    column_default
FROM information_schema.columns 
WHERE table_name = 'rog_location2025'
ORDER BY ordinal_position;

COMMENT ON TABLE rog_location2025 IS '2025年版チェックポイント管理テーブル';
COMMENT ON COLUMN rog_location2025.cp_number IS 'CP番号';
COMMENT ON COLUMN rog_location2025.event_id IS 'イベントID';
COMMENT ON COLUMN rog_location2025.cp_name IS 'CP名';
COMMENT ON COLUMN rog_location2025.latitude IS '緯度';
COMMENT ON COLUMN rog_location2025.longitude IS '経度';
COMMENT ON COLUMN rog_location2025.location IS '位置(PostGIS Point)';
COMMENT ON COLUMN rog_location2025.cp_point IS 'チェックポイント得点';
COMMENT ON COLUMN rog_location2025.photo_point IS '写真ポイント';
COMMENT ON COLUMN rog_location2025.buy_point IS '買い物ポイント';
COMMENT ON COLUMN rog_location2025.checkin_radius IS 'チェックイン範囲(m)';
COMMENT ON COLUMN rog_location2025.auto_checkin IS '自動チェックイン';
COMMENT ON COLUMN rog_location2025.shop_closed IS '休業中';
COMMENT ON COLUMN rog_location2025.shop_shutdown IS '閉業';
COMMENT ON COLUMN rog_location2025.opening_hours IS '営業時間';
COMMENT ON COLUMN rog_location2025.address IS '住所';
COMMENT ON COLUMN rog_location2025.phone IS '電話番号';
COMMENT ON COLUMN rog_location2025.website IS 'ウェブサイト';
COMMENT ON COLUMN rog_location2025.description IS '説明';
COMMENT ON COLUMN rog_location2025.is_active IS '有効';
COMMENT ON COLUMN rog_location2025.sort_order IS '表示順';
COMMENT ON COLUMN rog_location2025.csv_source_file IS 'CSVファイル名';
COMMENT ON COLUMN rog_location2025.csv_upload_date IS 'CSVアップロード日時';
COMMENT ON COLUMN rog_location2025.csv_upload_user_id IS 'CSVアップロードユーザーID';
COMMENT ON COLUMN rog_location2025.created_at IS '作成日時';
COMMENT ON COLUMN rog_location2025.updated_at IS '更新日時';
COMMENT ON COLUMN rog_location2025.created_by_id IS '作成者ID';
COMMENT ON COLUMN rog_location2025.updated_by_id IS '更新者ID';
