-- =============================================================================
-- bibimbap dev seed teardown — seed-dev.sql 로 만든 더미 데이터만 정리
-- =============================================================================
-- seed-dev.sql 이 생성한 더미(@bibimbap.local 유저 + '테스트 게임 (더미)' + 그
--   게임의 리뷰/댓글)만 hard delete 한다. 운영성 데이터는 건드리지 않는다.
--
-- 적용:
--   docker exec -i bibimbap-db psql -U bibimbap -d bibimbap -v ON_ERROR_STOP=1 < db/seed-dev-teardown.sql
--   PGPASSWORD=change_me_local_dev psql -h localhost -p 5433 -U bibimbap -d bibimbap -f db/seed-dev-teardown.sql
--
-- FK 의존 역순 삭제: reviews/comments/likes → games → auth_identities → users.
-- =============================================================================

SET search_path TO dev;

DO $$
DECLARE
    v_game_id bigint;
BEGIN
    SELECT id INTO v_game_id FROM games WHERE name = '테스트 게임 (더미)' LIMIT 1;

    IF v_game_id IS NOT NULL THEN
        DELETE FROM game_reviews  WHERE game_id = v_game_id;
        DELETE FROM game_comments WHERE game_id = v_game_id;
        DELETE FROM game_likes    WHERE game_id = v_game_id;
        DELETE FROM games         WHERE id = v_game_id;
    END IF;

    DELETE FROM user_auth_identities
    WHERE user_id IN (SELECT id FROM users WHERE canonical_email LIKE '%@bibimbap.local');

    DELETE FROM users WHERE canonical_email LIKE '%@bibimbap.local';

    RAISE NOTICE 'seed-dev teardown 완료 (game_id=%)', v_game_id;
END $$;

SELECT
    (SELECT count(*) FROM users WHERE canonical_email LIKE '%@bibimbap.local')        AS remaining_dummy_users,
    (SELECT count(*) FROM games WHERE name = '테스트 게임 (더미)')                       AS remaining_dummy_games;
