import { describe, expect, it, vi } from 'vitest';
import {
    BASE_QUICK_ACTIONS,
    buildQuickActions,
    cloneProfileDraft,
    normalizeName,
    normalizeProfileForApply
} from './ChatControl';

vi.mock('../lib/supabaseClient', () => ({
    supabase: {
        auth: {
            getSession: vi.fn(async () => ({ data: { session: null } }))
        }
    }
}));

describe('ChatControl helpers', () => {
    it('normalizes names for fuzzy profile matching', () => {
        expect(normalizeName('High Risk Scalper ⚡')).toBe('highriskscalper');
        expect(normalizeName('Conservative-Bag 01')).toBe('conservativebag01');
    });

    it('builds quick actions with profile-specific tune actions first', () => {
        const quick = buildQuickActions([
            { name: 'High Risk Scalper ⚡' },
            { name: 'Conservative Bag' }
        ]);

        expect(quick[0].label).toContain('Tune High Risk Scalper ⚡');
        expect(quick[1].label).toContain('Tune Conservative Bag');
        expect(quick.length).toBe(BASE_QUICK_ACTIONS.length + 2);
    });

    it('falls back to base quick actions when profile names do not match', () => {
        const quick = buildQuickActions([{ name: 'Balanced Trend Rider' }]);
        expect(quick).toEqual(BASE_QUICK_ACTIONS);
    });

    it('clones draft profile strategy config deeply', () => {
        const original = {
            name: 'AI Profile',
            strategy_config: {
                execution: { entryMode: 'both' },
                rules: [{ ruleId: 'TrendBiasRule', enabled: true }]
            }
        };

        const clone = cloneProfileDraft(original);
        clone.strategy_config.rules[0].enabled = false;

        expect(original.strategy_config.rules[0].enabled).toBe(true);
        expect(clone.strategy_config.rules[0].enabled).toBe(false);
    });

    it('normalizes profile payload fields before apply', () => {
        const normalized = normalizeProfileForApply({
            name: '  Test Profile ',
            allocated_capital: '2500',
            risk_per_trade_percent: '1.5',
            symbols: ' BTC/USDT ',
            is_active: true
        });

        expect(normalized.name).toBe('Test Profile');
        expect(normalized.allocated_capital).toBe(2500);
        expect(normalized.risk_per_trade_percent).toBe(1.5);
        expect(normalized.symbols).toBe('BTC/USDT');
        expect(normalized.is_active).toBe(true);
    });
});
