import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount, flushPromises } from '@vue/test-utils';
import { createVuetify } from 'vuetify';
import TopupDialog from '../../resources/js/components/billing/TopupDialog.vue';
import * as billingApi from '../../resources/js/api/billing';
import * as redirectUtil from '../../resources/js/utils/redirect';

vi.mock('../../resources/js/api/billing');
vi.mock('../../resources/js/utils/redirect');

const vuetify = createVuetify();

const factory = () =>
    mount(TopupDialog, {
        global: { plugins: [vuetify] },
        props: { modelValue: true },
    });

describe('TopupDialog.vue', () => {
    beforeEach(() => {
        vi.mocked(billingApi.topup).mockResolvedValue({
            transaction: {
                id: 1,
                type: 'topup',
                amount_rub: '5000.00',
                balance_rub_after: '5000.00',
                created_at: '2026-05-16T00:00:00Z',
            },
            balance_rub: '5000.00',
        });
    });

    it('блокирует submit при сумме ниже 100 ₽', async () => {
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number | null }).amount = 50;
        await flushPromises();
        expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(false);
    });

    it('разрешает submit при валидной сумме', async () => {
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number | null }).amount = 5000;
        await flushPromises();
        expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(true);
    });

    it('submit вызывает topup и эмитит success с новым балансом', async () => {
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number | null }).amount = 5000;
        await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
        await flushPromises();
        expect(billingApi.topup).toHaveBeenCalledWith(5000);
        expect(wrapper.emitted('success')?.[0]).toEqual(['5000.00']);
    });

    it('блокирует submit при нечисловом значении (очищенное поле)', async () => {
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number }).amount = NaN;
        await flushPromises();
        expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(false);
    });

    it('при ответе с confirmation_url делает редирект на оплату, без success', async () => {
        vi.mocked(billingApi.topup).mockResolvedValue({
            confirmation_url: 'https://yoomoney.ru/checkout/pay_x',
        });
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number | null }).amount = 5000;
        await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
        await flushPromises();
        expect(redirectUtil.redirectTo).toHaveBeenCalledWith('https://yoomoney.ru/checkout/pay_x');
        expect(wrapper.emitted('success')).toBeUndefined();
    });

    it('показывает ошибку при отказе backend', async () => {
        vi.mocked(billingApi.topup).mockRejectedValue(new Error('fail'));
        const wrapper = factory();
        (wrapper.vm as unknown as { amount: number | null }).amount = 5000;
        await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
        await flushPromises();
        expect((wrapper.vm as unknown as { errorMsg: string | null }).errorMsg).not.toBeNull();
    });
});
