CREATE EXTENSION IF NOT EXISTS pgcrypto;
DO $$
DECLARE _admin_id bigint;
_manager_id bigint;
_company_wallet_id bigint;
_company_id bigint;
_branch_id bigint;
_branch_wallet_id bigint;
_cashier_id bigint;
BEGIN
INSERT INTO users (
        first_name,
        last_name,
        email,
        phone_number,
        password,
        role,
        email_verified,
        phone_verified,
        created_at,
        updated_at,
        suspended
    )
VALUES (
        'Admin',
        'BetFidel',
        'admin.betfidel@gmail.com',
        NULL,
        crypt('password@123', gen_salt('bf'))::bytea,
        'admin',
        TRUE,
        FALSE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        FALSE
    ) ON CONFLICT (email) DO
UPDATE
SET updated_at = EXCLUDED.updated_at
RETURNING id INTO STRICT _admin_id;
INSERT INTO users (
        first_name,
        last_name,
        email,
        phone_number,
        password,
        role,
        email_verified,
        phone_verified,
        created_at,
        updated_at,
        suspended,
        company_id
    )
VALUES (
        'Manager',
        'BetFidel',
        'manager.betfidel@gmail.com',
        NULL,
        crypt('password@123', gen_salt('bf'))::bytea,
        'branch_manager',
        TRUE,
        FALSE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        FALSE,
        _company_id
    ) ON CONFLICT (email) DO
UPDATE
SET updated_at = EXCLUDED.updated_at
RETURNING id INTO STRICT _manager_id;
INSERT INTO wallets (
        balance,
        is_withdraw,
        is_bettable,
        is_transferable,
        user_id,
        type,
        currency,
        is_active,
        created_at,
        updated_at
    )
VALUES (
        10000,
        TRUE,
        TRUE,
        TRUE,
        _admin_id,
        'company_wallet',
        'ETB',
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    )
RETURNING id INTO STRICT _company_wallet_id;
INSERT INTO companies (
        name,
        slug,
        admin_id,
        wallet_id,
        deducted_percentage,
        is_active,
        created_at,
        updated_at
    )
VALUES (
        'FidelBet',
        'betfidel',
        _admin_id,
        _company_wallet_id,
        0.15,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    ) ON CONFLICT (slug) DO
UPDATE
SET updated_at = EXCLUDED.updated_at
RETURNING id INTO STRICT _company_id;
UPDATE users
SET company_id = _company_id
WHERE id = _admin_id;
INSERT INTO wallets (
        balance,
        is_withdraw,
        is_bettable,
        is_transferable,
        user_id,
        type,
        currency,
        is_active,
        created_at,
        updated_at
    )
VALUES (
        10000,
        TRUE,
        TRUE,
        TRUE,
        _admin_id,
        'branch_wallet',
        'ETB',
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    )
RETURNING id INTO STRICT _branch_wallet_id;
INSERT INTO branches (
        name,
        location,
        wallet_id,
        branch_manager_id,
        company_id,
        is_self_owned,
        profit_percent,
        is_active,
        created_at,
        updated_at
    )
VALUES (
        'Test Branch',
        'addis_ababa',
        _branch_wallet_id,
        _manager_id,
        _company_id,
        TRUE,
        0.10,
        TRUE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    ) ON CONFLICT (wallet_id) DO
UPDATE
SET updated_at = EXCLUDED.updated_at
RETURNING id INTO STRICT _branch_id;
INSERT INTO users (
        first_name,
        last_name,
        email,
        phone_number,
        password,
        role,
        email_verified,
        phone_verified,
        created_at,
        updated_at,
        suspended,
        company_id
    )
VALUES (
        'Cashier',
        'BetFidel',
        'cashier.betfidel@gmail.com',
        NULL,
        crypt('password@123', gen_salt('bf'))::bytea,
        'cashier',
        TRUE,
        FALSE,
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        FALSE,
        _company_id
    ) ON CONFLICT (email) DO
UPDATE
SET updated_at = EXCLUDED.updated_at
RETURNING id INTO STRICT _cashier_id;
INSERT INTO branch_cashiers (user_id, branch_id)
VALUES (_cashier_id, _branch_id);
RAISE NOTICE 'BETFIDEL_DEV_DATA (Admin ID: %, Company Wallet ID: %, Company ID: %)',
_admin_id,
_company_wallet_id,
_company_id;
RAISE NOTICE 'BETFIDEL_DEV_DATA (Branch ID: %, Branch Wallet ID: %, Manager ID: %)',
_branch_id,
_branch_wallet_id,
_manager_id;
RAISE NOTICE 'BETFIDEL_DEV_DATA (Cashier ID: %)',
_cashier_id;
END $$;