import * as toast from '@theorchard/suite-components';
import { useParams } from 'react-router-dom';
import { contractFlowthroughDetail } from 'src/__fixtures__/graphql/contract-flowthrough';
import * as abacusUpdateContractFlowthrough from 'src/apollo/mutations/contract-flowthrough';
import { useSaveHandler } from '../contract-flowthrough/update-contract-flowthrough';
import type { AbacusContractFlowthroughUpdate } from 'src/apollo/definitions/globalTypes';

jest.mock('react-router-dom', () => ({
    ...jest.requireActual('react-router-dom'),
    useParams: jest.fn(),
}));

describe('useSaveHandler (update contract flowthrough)', () => {
    let mockUpdateContractFlowthrough: jest.Mock;
    let updateContractFlowthroughSpy: jest.SpyInstance;
    let useToastSpy: jest.SpyInstance;

    const mockResponse = {
        data: {
            abacusUpdateContractFlowthrough: {
                contractFlowthroughId: '123',
            },
        },
    };

    beforeEach(() => {
        mockUpdateContractFlowthrough = jest
            .fn()
            .mockResolvedValue(mockResponse);

        updateContractFlowthroughSpy = jest
            .spyOn(
                abacusUpdateContractFlowthrough,
                'useUpdateContractFlowthrough'
            )
            .mockReturnValue(mockUpdateContractFlowthrough);

        useToastSpy = jest.spyOn(toast, 'useToast').mockReturnValue(jest.fn());
        (useParams as jest.Mock).mockReturnValue({ contractId: '123' });
    });

    afterEach(jest.restoreAllMocks);

    const fixture = contractFlowthroughDetail.abacusContractFlowthrough;

    const contractFlowthrough: AbacusContractFlowthroughUpdate = {
        flowthroughRate: fixture.flowthroughRate,
        hasAutomaticShutoff: fixture.hasAutomaticShutoff,
        recoupmentCap: fixture.recoupmentCap,
        referenceFlowthroughCalculationId:
            fixture.referenceFlowthroughCalculation
                .referenceFlowthroughCalculationId,
        contractFlowthroughId: '123',
    };

    it('calls mutation with values from fixture and handles result', async () => {
        const saveHandler = useSaveHandler();

        const result = await saveHandler(contractFlowthrough);

        expect(updateContractFlowthroughSpy).toHaveBeenCalledWith('123');
        expect(mockUpdateContractFlowthrough).toHaveBeenCalledWith({
            variables: {
                input: {
                    flowthroughRate: 12.1,
                    hasAutomaticShutoff: true,
                    recoupmentCap: 12901,
                    referenceFlowthroughCalculationId: '1',
                    contractFlowthroughId: '123',
                },
            },
        });
        expect(result).toBeUndefined();
        expect(useToastSpy).toHaveBeenCalled();
    });

    it('sets recoupmentCap to null if not provided', async () => {
        const saveHandler = useSaveHandler();
        const result = await saveHandler({
            flowthroughRate: 12.1,
            hasAutomaticShutoff: true,
            referenceFlowthroughCalculationId:
                fixture.referenceFlowthroughCalculation
                    .referenceFlowthroughCalculationId,
            contractFlowthroughId: '123',
        });
        expect(mockUpdateContractFlowthrough).toHaveBeenCalledWith({
            variables: {
                input: {
                    flowthroughRate: 12.1,
                    hasAutomaticShutoff: true,
                    recoupmentCap: null,
                    referenceFlowthroughCalculationId:
                        fixture.referenceFlowthroughCalculation
                            .referenceFlowthroughCalculationId,
                    contractFlowthroughId: '123',
                },
            },
        });
        expect(result).toBeUndefined();
        expect(useToastSpy).toHaveBeenCalled();
    });

    it('sets recoupmentCap if provided', async () => {
        const saveHandler = useSaveHandler();
        const result = await saveHandler({
            flowthroughRate: 12.1,
            hasAutomaticShutoff: true,
            recoupmentCap: 10000,
            referenceFlowthroughCalculationId:
                fixture.referenceFlowthroughCalculation
                    .referenceFlowthroughCalculationId,
            contractFlowthroughId: '123',
        });
        expect(mockUpdateContractFlowthrough).toHaveBeenCalledWith({
            variables: {
                input: {
                    flowthroughRate: 12.1,
                    hasAutomaticShutoff: true,
                    recoupmentCap: 10000,
                    referenceFlowthroughCalculationId:
                        fixture.referenceFlowthroughCalculation
                            .referenceFlowthroughCalculationId,
                    contractFlowthroughId: '123',
                },
            },
        });
        expect(result).toBeUndefined();
        expect(useToastSpy).toHaveBeenCalled();
    });
});
