import { TrackOfferType } from 'src/data/globalTypes';
import { MergedProduct } from 'src/product/product';
import type { LockReviewQueueItemMutation } from 'src/data/mutations/lockReviewQueueItem/__generated__/LockReviewQueueItemMutation';
import type {
    Product,
    ProductTrack,
    Track,
} from 'src/data/queries/product/types';
import type { ProductValidationQuery } from 'src/data/queries/productValidation/__generated__/ProductValidationQuery';
import type { ReviewQueueSearchResults } from 'src/data/queries/reviewQueueSearch/types';
import type {
    ProductValidationErrors,
    TrackValidationErrors,
    TrackValidationWarnings,
    Validation,
} from 'src/types';

export const getMockProductTrack = (
    trackFields?: Partial<ProductTrack>
): ProductTrack => ({
    __typename: 'Track',
    tuid: '123456',
    trackNumber: 1,
    volumeNumber: 1,
    durationMinutes: 0,
    durationSeconds: 2,
    trackName: 'House Of The Rising Sun',
    version: '1',
    isrc: 'QM6MZ2049475',
    offerType: TrackOfferType.NONE,
    ownershipRights: 'original_owner',
    lyrics: 'There is a house in New Orleans',
    primaryArtists: [
        { artistName: 'Gio Pika', artistType: 'performer' },
        { artistName: 'John Snow', artistType: 'producer' },
        { artistName: 'John Snow', artistType: 'writer' },
        { artistName: 'Dustin Winterlake', artistType: 'feature_to_primary' },
        { artistName: 'Peter Piper', artistType: 'remixer' },
        { artistName: 'Rhyming Iris', artistType: 'featuring' },
    ],
    writers: [
        { trackWriterName: 'Sianaloha', trackWriterType: 'writer' },
        { trackWriterName: 'John Ice', trackWriterType: 'writer' },
    ],
    performers: null,
    pInfo: '2014 Fierce Panda Records',
    explicit: 'N',
    previewStartTime: null,
    language: {
        code: 'ENG',
        name: 'English',
    },
    instantGrats: null,
    labelSoundRecording: {
        participations: [
            {
                participant: {
                    name: 'Pete Sax',
                },
                role: {
                    category: {
                        name: 'x',
                    },
                    name: 'Author',
                },
            },
        ],
    },
    localizations: [],
    participations: [],
    isrcInfo: {
        isrc: 'QM6MZ2049475',
        tracks: {
            totalCount: 12,
            tracks: [{ tuid: '255' }, { tuid: '256' }],
        },
    },
    focusTrack: null,
    audioAttributes: [
        {
            attribute: {
                id: '1',
                description: 'N/A',
            },
        },
    ],
    audioAttributesEdits: [
        {
            attribute: {
                id: '1',
                description: 'N/A',
            },
        },
    ],
    suggestedAudioAttributes: [],
    rightsAttributes: [
        {
            attribute: {
                id: '1',
                description: 'Public Domain',
            },
        },
    ],
    rightsAttributesEdits: [
        {
            attribute: {
                id: '1',
                description: 'Public Domain',
            },
        },
    ],
    suggestedRightsAttributes: [],
    ...trackFields,
});

export const getMockTrack = (trackFields?: Partial<Track>): Track => ({
    __typename: 'Track',
    tuid: '123456',
    trackNumber: 1,
    durationMinutes: 0,
    durationSeconds: 2,
    trackName: 'House Of The Rising Sun',
    streamingAudioUrl: 'http://example.com',
    ...trackFields,
});

export const getProductMetadata = (
    productFields: Partial<Product> = {}
): Product =>
    ({
        __typename: 'Product',
        productId: 33,
        productCode: 'KASHWIN17S1',
        productName: 'Wubba Lubba Dub Dub',
        imprint: 'Kash Money Inc.',
        upc: '191773836184',
        deliveredVersion: 'the best version',
        format: 'Single',
        metadataLanguage: {
            code: 'UKR',
            name: 'Ukrainian',
        },
        labelParticipations: [],
        productLocalizations: [],
        cLine: '2017 Kash Money Inc',
        genreId: 2,
        subgenreId: 33,
        version: 'Version',
        specialInstructions: 'Special instructions',
        notForDistribution: 'Not for distribution',
        imageLocation: 'http:/path/to/img',
        label: {
            id: {
                vendorId: 123,
                subaccountId: 333,
                __typename: 'LabelId',
            },
            name: 'Some subaccount name',
            vendor: {
                name: 'Some vendor name',
                serviceTier: null,
                __typename: 'Vendor',
            },
            __typename: 'Subaccount',
        },
        reviewHistory: {
            items: [
                {
                    createdDatetime: '2024-11-14T13:56:12.160258Z',
                    destinationQueueName: null,
                    escalationType: null,
                    note: 'good to go',
                    queueName: 'initial',
                    reviewQueueId: 1,
                    submissionCount: 1,
                    submissionType: 'new',
                    userAction: 'approval',
                    userInfo: {
                        id: '4444',
                        name: 'User ABC',
                    },
                },
                {
                    createdDatetime: '2024-11-10T10:00:00',
                    destinationQueueName: 'escalation',
                    escalationType: 'Example Type 1',
                    note: 'there is something wrong, take a look',
                    queueName: 'initial',
                    reviewQueueId: 1,
                    submissionCount: 1,
                    submissionType: 'new',
                    userAction: 'queueMove',
                    userInfo: {
                        id: '1001',
                        name: 'User Y',
                    },
                },
                {
                    createdDatetime: '2024-05-14T19:19:26.000000Z',
                    destinationQueueName: null,
                    escalationType: null,
                    note: null,
                    queueName: 'initial',
                    reviewQueueId: 1,
                    submissionCount: 1,
                    submissionType: 'new',
                    userAction: 'submission',
                    userInfo: null,
                },
            ],
            totalCount: 2,
        },
        tracks: [getMockProductTrack()],
        subgenre: {
            id: 222,
            name: 'subgenre name',
            genre: { id: 123, name: 'Genre test name' },
        },
        artists: [
            {
                artistId: '4325',
                artistName: 'Michael Jackson',
                artistType: 'writer',
            },
        ],
        releaseDate: '2020-04-20',
        preorderDate: '2020-04-21',
        previewable: 'Yes',
        saleStartDate: '2020-05-22',
        releaseCorrection: {
            items: [
                {
                    keyId: 4132816,
                    keyValue: '"http:/path/to/revised/img"',
                    tableName: 'releases',
                    fieldName: 'coverart',
                },
            ],
        },
        ...productFields,
    } as Product);

export const getProductMetadataWithMissingTrackAudioAttributes = (
    productFields: Partial<Product> = {}
): Product =>
    ({
        __typename: 'Product',
        productId: 44,
        productCode: 'KASHWIN17S1',
        productName: 'Wubba Lubba Dub Dub',
        imprint: 'Kash Money Inc.',
        upc: '191773836184',
        deliveredVersion: 'the best version',
        format: 'Single',
        metadataLanguage: {
            code: 'UKR',
            name: 'Ukrainian',
        },
        cLine: '2017 Kash Money Inc',
        genreId: 2,
        subgenreId: 33,
        version: 'Version',
        specialInstructions: 'Special instructions',
        notForDistribution: 'Not for distribution',
        imageLocation: 'http:/path/to/img',
        label: {
            id: {
                vendorId: 123,
                subaccountId: 333,
                __typename: 'LabelId',
            },
            name: 'Some subaccount name',
            vendor: {
                name: 'Some vendor name',
                serviceTier: null,
                __typename: 'Vendor',
            },
            __typename: 'Subaccount',
        },
        tracks: [
            getMockProductTrack({
                audioAttributes: [],
            }),
        ],
        subgenre: {
            id: 222,
            name: 'subgenre name',
            genre: { id: 123, name: 'Genre test name' },
        },
        artists: [
            {
                artistId: '4325',
                artistName: 'Michael Jackson',
                artistType: 'writer',
            },
        ],
        releaseDate: '2020-04-20',
        preorderDate: '2020-04-21',
        previewable: 'Yes',
        saleStartDate: '2020-05-22',
        releaseCorrection: {
            items: [
                {
                    keyId: 4132816,
                    keyValue: '"http:/path/to/revised/img"',
                    tableName: 'releases',
                    fieldName: 'coverart',
                },
            ],
        },
        ...productFields,
    } as Product);

export const getProductMetadataWithMissingTrackRightsAttributes = (
    productFields: Partial<Product> = {}
): Product =>
    ({
        __typename: 'Product',
        productId: 44,
        productCode: 'KASHWIN17S1',
        productName: 'Wubba Lubba Dub Dub',
        imprint: 'Kash Money Inc.',
        upc: '191773836184',
        deliveredVersion: 'the best version',
        format: 'Single',
        metadataLanguage: {
            code: 'UKR',
            name: 'Ukrainian',
        },
        cLine: '2017 Kash Money Inc',
        genreId: 2,
        subgenreId: 33,
        version: 'Version',
        specialInstructions: 'Special instructions',
        notForDistribution: 'Not for distribution',
        imageLocation: 'http:/path/to/img',
        label: {
            id: {
                vendorId: 123,
                subaccountId: 333,
                __typename: 'LabelId',
            },
            name: 'Some subaccount name',
            vendor: {
                name: 'Some vendor name',
                serviceTier: null,
                __typename: 'Vendor',
            },
            __typename: 'Subaccount',
        },
        tracks: [
            getMockProductTrack({
                rightsAttributes: [],
            }),
        ],
        subgenre: {
            id: 222,
            name: 'subgenre name',
            genre: { id: 123, name: 'Genre test name' },
        },
        artists: [
            {
                artistId: '4325',
                artistName: 'Michael Jackson',
                artistType: 'writer',
            },
        ],
        releaseDate: '2020-04-20',
        preorderDate: '2020-04-21',
        previewable: 'Yes',
        saleStartDate: '2020-05-22',
        releaseCorrection: {
            items: [
                {
                    keyId: 4132816,
                    keyValue: '"http:/path/to/revised/img"',
                    tableName: 'releases',
                    fieldName: 'coverart',
                },
            ],
        },
        ...productFields,
    } as Product);

export const getMergedProduct = (
    productFields: Partial<Product> = {},
    validations: Validation[] = []
): MergedProduct => {
    const productData = getProductMetadata(productFields);
    return new MergedProduct(productData, null, null, validations);
};

export const getReviewQueueSearchData = ({
    itemsChanges = [],
    totalCount,
}: {
    itemsChanges?: { reviewQueueId?: number; productId?: number }[];
    totalCount?: number;
} = {}) =>
    ({
        totalCount: totalCount ?? itemsChanges.length,
        queueCounts: [{ queueName: 'initial', count: itemsChanges.length }],
        items: Array.from({ length: itemsChanges.length }, (_, index) => ({
            accountId: '123:333',
            accountName: 'Label A',
            addedAt: '2023-02-09T13:24:17.000000Z',
            assignedToId: 2181,
            assignedToName: 'Reviewer 12321',
            assignedReviewerId: 1812,
            assignedReviewerName: 'Reviewer 32123',
            escalationType: null,
            featuringArtist: '',
            format: 'Single',
            imageLocation: 'http:/path/to/img',
            imprint: 'Kash Money Inc.',
            labelId: 123,
            labelName: 'Mixus Joint Stock Company',
            preorderDate: '2020-04-21',
            primaryArtist: 'Michael Jackson',
            productId: 2,
            productName: 'Wubba Lubba Dub Dub',
            queueName: 'initial',
            submissionCount: 1,
            submissionType: 'New',
            reviewQueueId: 33,
            saleStartDate: '2020-05-22',
            secondaryInternalContactName: 'Bar',
            specialInstructions: 'Special instructions',
            serviceTierName: null,
            serviceTierUuid: null,
            subaccountId: 333,
            subaccountName: 'Subaccount name',
            submissionDatetime: '2023-02-09T13:24:17.000000Z',
            upc: '191773836184',
            metadataLanguageName: 'Ukrainian',
            movedToQueueByUserName: null,
            movedToTargetId: null,
            movedToTargetName: null,
            movedToTargetEmail: null,
            notices: [],
            ...itemsChanges[index],
            __typename: 'ReviewQueueSearchResult',
        })),
        filterOptions: {
            accountCountry: [
                { id: 'US', name: 'USA', __typename: 'FilterOption' },
            ],
            accountId: [
                {
                    id: '123:333',
                    name: 'Label A',
                    extra: null,
                    __typename: 'FilterOption',
                },
            ],
            assignedToId: [
                {
                    id: '2181',
                    name: 'Reviewer 12321',
                    __typename: 'FilterOption',
                },
            ],
            assignedReviewerId: [
                {
                    id: '1812',
                    name: 'Reviewer 32123',
                    __typename: 'FilterOption',
                },
            ],
            bulkSessionId: [],
            submissionType: [
                { id: 'New', name: 'New', __typename: 'FilterOption' },
            ],
            serviceTierUuid: [],
            queueName: [],
            genreId: [],
            movedById: [],
            companyBrandUuid: [],
            metadataLanguageCode: [
                {
                    id: 'UKR',
                    name: 'Ukrainian',
                    __typename: 'FilterOption',
                },
            ],
            movedToTargetId: [],
            noticeCode: [],
            __typename: 'ReviewQueueSearchFilterOptions',
        },
        __typename: 'ReviewQueueSearchResults',
    } as ReviewQueueSearchResults);

export const getProductValidationData = ({
    productId = 33,
    productErrors = [],
    trackErrors = [],
    trackWarnings = [],
}: {
    productId?: number;
    productErrors?: ProductValidationErrors;
    trackErrors?: TrackValidationErrors;
    trackWarnings?: TrackValidationWarnings;
} = {}): ProductValidationQuery['product'] => ({
    __typename: 'Product',
    productId,
    validation: {
        errors: productErrors,
        warnings: [],
    },
    tracks: [
        {
            __typename: 'Track',
            tuid: '123456',
            validation: {
                errors: trackErrors,
                warnings: trackWarnings,
            },
        },
    ],
});

export const getProductPricingData = ({ productId = 112 } = {}) => ({
    __typename: 'Product',
    productId,
    albumPricingTier: {
        pricingFamily: 'MUSIC_ALBUM',
        orchardPricingTier: {
            id: '15',
            name: 'Mid/Front',
        },
        productOverrides: [
            {
                intervalEnd: '12/13/2024',
                intervalStart: '10/13/2023',
                orchardPricingTier: {
                    id: '16',
                    name: 'Mid',
                },
            },
        ],
    },
    trackPricingTier: {
        pricingFamily: 'MUSIC_TRACK',
        orchardPricingTier: {
            id: '26',
            name: 'Mid',
        },
        trackOverrides: [
            {
                trackId: '35390339',
                orchardPricingTier: {
                    id: '26',
                    name: 'Mid',
                },
            },
            {
                trackId: '35478981',
                orchardPricingTier: {
                    id: '27',
                    name: 'Back',
                },
            },
        ],
    },
});

export const getClassicalProductData = () => ({
    subgenre: {
        id: 149,
        name: 'Classical',
        genre: { id: 12, name: 'Classical' },
    },
    artists: [
        {
            artistId: '4320',
            artistName: 'Johann Sebastian Bach',
            artistType: 'performer',
        },
        {
            artistId: '4321',
            artistName: 'Helen',
            artistType: 'composer',
        },
        {
            artistId: '4322',
            artistName: 'Mario',
            artistType: 'conductor',
        },
        {
            artistId: '4323',
            artistName: 'Group bear',
            artistType: 'ensemble',
        },
        {
            artistId: '4324',
            artistName: 'Group honey',
            artistType: 'orchestra',
        },
        {
            artistId: '4325',
            artistName: 'Group Bobby',
            artistType: 'producer',
        },
    ],
    tracks: [
        getMockProductTrack({
            primaryArtists: [
                {
                    artistName: 'Johann Sebastian Bach',
                    artistType: 'performer',
                },
                {
                    artistName: 'Helen',
                    artistType: 'composer',
                },
                {
                    artistName: 'Mario',
                    artistType: 'conductor',
                },
                {
                    artistName: 'Group bear',
                    artistType: 'ensemble',
                },
                {
                    artistName: 'Group honey',
                    artistType: 'orchestra',
                },
            ],
        }),
    ],
});

type NonNullableLockReviewQueueItem = Required<{
    [K in keyof LockReviewQueueItemMutation['lockReviewQueueItem']]: NonNullable<
        LockReviewQueueItemMutation['lockReviewQueueItem'][K]
    >;
}>;

export const getLockReviewQueueItemData = (
    lockReviewQueueItemFields: Partial<NonNullableLockReviewQueueItem> = {}
): NonNullableLockReviewQueueItem => ({
    __typename: 'ReviewQueueItem',
    lockedByUserId: 'e35b0c43-35b7-416e-aa15-105900184f06',
    lockedUntilDatetime: '2022-10-26T16:44:02.267741Z',
    ...lockReviewQueueItemFields,
});

export const getUnlockReviewQueueItemData = () => ({
    lockedByUserId: 'e35b0c43-35b7-416e-aa15-105900184f06',
    lockedUntilDatetime: '2022-10-26T16:40:11.545144Z',
});

export const getReviewQueueItemData = (reviewQueueId: number) => ({
    reviewQueueId,
    productId: 1,
    queueName: 'initial',
    submissionType: 'new',
    movedToTarget: null,
    movedToQueueByIdentity: null,
    movedAtDatetime: null,
    moveNote: null,
});
