package ru.otus.mobile.util;

public enum TestAccount {
    WISHLISTS("hw7_wishlists_user", null),
    GIFTS("hw7_gifts_user", null),
    RESERVATION("hw7_reservation_user", """
            WITH owner_user AS (
              SELECT id FROM users WHERE username = ?
            ),
            owner_insert AS (
              INSERT INTO users (id, email, password, username)
              SELECT gen_random_uuid(), ?, ?, ?
              WHERE NOT EXISTS (SELECT 1 FROM owner_user)
              RETURNING id
            ),
            owner AS (
              SELECT id FROM owner_user
              UNION ALL
              SELECT id FROM owner_insert
            ),
            target_user_existing AS (
              SELECT id FROM users WHERE username = ?
            ),
            target_user_insert AS (
              INSERT INTO users (id, email, password, username)
              SELECT gen_random_uuid(), ?, ?, ?
              WHERE NOT EXISTS (SELECT 1 FROM target_user_existing)
              RETURNING id
            ),
            target_user AS (
              SELECT id FROM target_user_existing
              UNION ALL
              SELECT id FROM target_user_insert
            ),
            del_owner_gifts AS (
              DELETE FROM gifts
              WHERE wish_id IN (SELECT id FROM wishlists WHERE user_id = (SELECT id FROM owner))
            ),
            del_owner_wishlists AS (
              DELETE FROM wishlists WHERE user_id = (SELECT id FROM owner)
            ),
            del_user_gifts AS (
              DELETE FROM gifts
              WHERE wish_id IN (SELECT id FROM wishlists WHERE user_id IN (SELECT id FROM target_user))
            ),
            del_user_wishlists AS (
              DELETE FROM wishlists WHERE user_id IN (SELECT id FROM target_user)
            ),
            owner_wishlist AS (
              INSERT INTO wishlists (id, title, description, user_id)
              SELECT gen_random_uuid(), 'Owner Wishlist', 'Auto', (SELECT id FROM owner)
              WHERE EXISTS (SELECT 1 FROM owner)
              RETURNING id
            ),
            owner_gift AS (
              INSERT INTO gifts (id, name, description, price, is_reserved, store_url, image_url, wish_id)
              SELECT gen_random_uuid(), 'Owner Gift', 'Auto', 100, false, NULL, NULL, (SELECT id FROM owner_wishlist)
            )
            SELECT 1;
            """);

    private final String username;
    private final String resetSql;

    TestAccount(String username, String resetSql) {
        this.username = username;
        this.resetSql = resetSql;
    }

    public String username() {
        return username;
    }

    public String resetSql() {
        return resetSql;
    }
}
