import { useToast } from '@theorchard/suite-frontend';
import React from 'react';
import {
    MarketingHighlightStore,
    MarketingProgramInfoEntity,
    PriorityType,
} from 'shared/data/schema';
import useMarketingSectionConfig from 'src/config';
import {
    useUpdateProductInternalNoteMutation,
    useUpdateProductMarketingPrioritiesMutation,
    useUpdateProjectMarketingHighlightsMutation,
    useUpdateProjectMarketingPrioritiesMutation,
    useUpsertMarketingProgramInfoMutation,
} from 'src/data/mutations';
import {
    useGetMarketingTerritories,
    useGetProductMarketingData,
    useGetProjectMarketingData,
} from 'src/data/queries';
import { PriorityIdType } from 'src/types';
import MarketingSectionOAPanel from './marketing-section-oa-panel';
import type {
    MarketingHighlight,
    MarketingProgramInfo,
    TerritoryMarketingPriority,
} from 'src/types';

const MarketingSectionOAPanelContainer = () => {
    const { projectId, productId, editable, onEdit } =
        useMarketingSectionConfig();
    const toast = useToast();

    const productEntityType =
        MarketingProgramInfoEntity.MARKETING_PROGRAM_INFO_PRODUCT;
    const {
        loading: loadingMarketingTerritories,
        data: marketingTerritories,
        error: errorMarketingTerritories,
    } = useGetMarketingTerritories();
    const {
        loading: loadingProductMarketingData,
        data: productMarketingData,
        error: errorProductMarketingData,
    } = useGetProductMarketingData(productId);
    const {
        loading: loadingProjectMarketingData,
        data: projectMarketingData,
        error: errorProjectMarketingData,
    } = useGetProjectMarketingData(projectId);

    const [
        updateProductInternalNoteMutation,
        {
            error: errorUpdateProductInternalNote,
            loading: loadingUpdateProductInternalNote,
        },
    ] = useUpdateProductInternalNoteMutation();
    const [
        updateProductMarketingPrioritiesMutation,
        {
            error: errorUpdateProductMarketingPriorities,
            loading: loadingUpdateProductMarketingPriorities,
        },
    ] = useUpdateProductMarketingPrioritiesMutation();
    const [
        updateProjectMarketingPrioritiesMutation,
        {
            error: errorUpdateProjectMarketingPriorities,
            loading: loadingUpdateProjectMarketingPriorities,
        },
    ] = useUpdateProjectMarketingPrioritiesMutation();
    const [
        updateProjectMarketingHighlightsMutation,
        {
            error: errorUpdateProjectMarketingHighlights,
            loading: loadingUpdateProjectMarketingHighlights,
        },
    ] = useUpdateProjectMarketingHighlightsMutation();
    const [
        upsertMarketingProgramInfoMutation,
        {
            error: errorUpsertMarketingProgramInfo,
            loading: loadingUpsertMarketingProgramInfoMutation,
        },
    ] = useUpsertMarketingProgramInfoMutation();

    const updateProductInternalNote =
        (productId: string) => (internalNote: string) =>
            updateProductInternalNoteMutation({
                variables: {
                    input: {
                        productId: parseInt(productId, 10),
                        internalNote: internalNote ?? '',
                    },
                },
            });

    const updateProductMarketingPriorities =
        (productId: string): any =>
        (productMarketingPriority: TerritoryMarketingPriority[]) =>
            updateProductMarketingPrioritiesMutation({
                variables: {
                    input: {
                        productId: parseInt(productId, 10),
                        priorities:
                            productMarketingPriority?.map(priority => ({
                                priorityId:
                                    priority.priorityIdType ===
                                    PriorityIdType.Product
                                        ? priority.priorityId
                                        : undefined,
                                priorityType:
                                    PriorityType[priority.priorityType],
                                territoryId: priority.territoryId,
                            })) ?? [],
                    },
                },
            });

    const updateProjectMarketingPriorities =
        (projectId: string): any =>
        (projectMarketingPriority: TerritoryMarketingPriority[]) =>
            updateProjectMarketingPrioritiesMutation({
                variables: {
                    input: {
                        projectId: parseInt(projectId, 10),
                        priorities:
                            projectMarketingPriority?.map(priority => ({
                                priorityId:
                                    priority.priorityIdType ===
                                    PriorityIdType.Project
                                        ? priority.priorityId
                                        : undefined,
                                priorityType:
                                    PriorityType[priority.priorityType],
                                territoryId: priority.territoryId,
                            })) ?? [],
                    },
                },
            });

    const updateProjectMarketingHighlights =
        (projectId: string): any =>
        (projectMarketingHighlights: MarketingHighlight[]) =>
            updateProjectMarketingHighlightsMutation({
                variables: {
                    delete: true,
                    input: {
                        projectId: parseInt(projectId, 10),
                        data: projectMarketingHighlights?.map(highlight => ({
                            marketingHighlight: highlight.marketingHighlight,
                            countryId: highlight.countryId
                                ? parseInt(highlight.countryId, 10)
                                : undefined,
                            store: highlight.store
                                ? MarketingHighlightStore[highlight.store]
                                : undefined,
                        })),
                    },
                },
            });

    const updateMarketingProgramInfo =
        (entityId: string): any =>
        (marketingProgramInfo: MarketingProgramInfo[]) =>
            upsertMarketingProgramInfoMutation({
                variables: {
                    input: {
                        entityId: parseInt(entityId, 10),
                        entityType: productEntityType,
                        data: marketingProgramInfo.map(info => ({
                            ...info,
                            entityType: undefined,
                            dateAdded: undefined,
                            __typename: undefined,
                        })),
                    },
                },
            });

    const graphqlState = {
        product: productMarketingData,
        project: projectMarketingData,
        territories: marketingTerritories,
    };

    return (
        <MarketingSectionOAPanel
            editable={editable}
            onEdit={onEdit}
            loading={
                loadingMarketingTerritories ||
                loadingProductMarketingData ||
                loadingProjectMarketingData ||
                loadingUpdateProductInternalNote ||
                loadingUpdateProductMarketingPriorities ||
                loadingUpdateProjectMarketingPriorities ||
                loadingUpdateProjectMarketingHighlights ||
                loadingUpsertMarketingProgramInfoMutation
            }
            error={
                !!errorMarketingTerritories ||
                !!errorProductMarketingData ||
                !!errorProjectMarketingData ||
                !!errorUpdateProductInternalNote ||
                !!errorUpdateProductMarketingPriorities ||
                !!errorUpdateProjectMarketingPriorities ||
                !!errorUpdateProjectMarketingHighlights ||
                !!errorUpsertMarketingProgramInfo
            }
            productId={productId}
            graphqlState={graphqlState}
            updateProductInternalNote={updateProductInternalNote(productId)}
            updateProductMarketingPriorities={updateProductMarketingPriorities(
                productId
            )}
            updateProductMarketingProgramInfo={updateMarketingProgramInfo(
                productId
            )}
            updateProjectMarketingPriorities={updateProjectMarketingPriorities(
                projectId
            )}
            updateProjectMarketingHighlights={updateProjectMarketingHighlights(
                projectId
            )}
            toast={toast}
        />
    );
};

export default MarketingSectionOAPanelContainer;
