import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { printAsset, accountBalance, waitForNextOpen } from './trading';
import type { Alpaca } from './alpaca';

function mockAlpaca(overrides: Partial<Alpaca> = {}): Alpaca {
    return {
        getAccount: vi.fn(),
        getAssets: vi.fn(),
        getAsset: vi.fn(),
        getClock: vi.fn(),
        getLatestQuote: vi.fn(),
        getLatestTrades: vi.fn(),
        ...overrides,
    } as unknown as Alpaca;
}

beforeEach(() => {
    vi.useFakeTimers();
});

afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
});

describe('printAsset', () => {
    beforeEach(() => {
        vi.spyOn(console, 'log').mockImplementation(() => {});
    });

    it('logs fractional when asset is fractionable', async () => {
        const alpaca = mockAlpaca({
            getAsset: vi.fn().mockResolvedValue({ symbol: 'TQQQ', fractionable: true, status: 'active', asset_class: 'us_equity' }),
        });

        await printAsset(alpaca, 'TQQQ');
        expect(console.log).toHaveBeenCalledWith('TQQQ is fractional');
    });

    it('logs not fractional when asset is not fractionable', async () => {
        const alpaca = mockAlpaca({
            getAsset: vi.fn().mockResolvedValue({ symbol: 'BRK.A', fractionable: false, status: 'active', asset_class: 'us_equity' }),
        });

        await printAsset(alpaca, 'BRK.A');
        expect(console.log).toHaveBeenCalledWith('BRK.A is not fractional');
    });
});

describe('accountBalance', () => {
    it('returns the cash value from the account', async () => {
        const alpaca = mockAlpaca({
            getAccount: vi.fn().mockResolvedValue({ cash: '10000.00' }),
        });

        const result = await accountBalance(alpaca);
        expect(result).toBe('10000.00');
    });
});

describe('waitForNextOpen', () => {
    it('calls getClock and waits until next_open', async () => {
        const futureDate = new Date(Date.now() + 60000).toISOString();
        const alpaca = mockAlpaca({
            getClock: vi.fn().mockResolvedValue({ is_open: false, next_open: futureDate, next_close: futureDate }),
        });

        const promise = waitForNextOpen(alpaca);
        await vi.advanceTimersByTimeAsync(60000);
        await promise;

        expect(alpaca.getClock).toHaveBeenCalled();
    });
});

