import { render } from '@testing-library/react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import React from 'react';
import DeliveryRights from '../delivery-rights';
import type { AccountFingerprintRules, FingerprintRule } from 'src/types';
import type { DeliveryRightsProps, Policy } from '../delivery-rights';

dayjs.extend(utc);

describe('<DeliveryRights>', () => {
    const vendorRules = [
        {
            active: true,
            policy: 'Monetize',
            service: 'TikTok',
            territory: 'FR',
            startDate: dayjs
                .utc('2023-01-02T00:00:00.000Z')
                .format('D MMM YYYY HH:mm:ss'),
            endDate: '-',
            createdDate: dayjs
                .utc('2023-01-01T00:00:00.000Z')
                .format('D MMM YYYY HH:mm:ss'),
            lastModifiedDate: dayjs
                .utc('2023-01-01T00:00:00.000Z')
                .format('D MMM YYYY HH:mm:ss'),
            createdBy: {
                profileId: '123',
                profileType: 'ContentProfile',
                profileName: 'Name',
            },
            lastModifiedBy: {
                profileId: '123',
                profileType: 'ContentProfile',
                profileName: 'Name',
            },
        },
    ] as FingerprintRule[];
    const subaccountRules = [] as FingerprintRule[];
    const territories = [
        {
            territoryCodeA2: 'FR',
            territoryName: 'France',
            continent: 'Europe',
        },
        {
            territoryCodeA2: 'US',
            territoryName: 'United States of America',
            continent: 'North America',
        },
    ];
    const accountRules = {
        accountType: 'Vendor',
        vendorRules,
        subaccountRules,
    } as AccountFingerprintRules;

    const defaultProps: DeliveryRightsProps = {
        rules: accountRules,
        territories,
        onCountryChange: (event: Policy) => event,
    };

    const renderWrapper = (props: Partial<DeliveryRightsProps> = {}) =>
        render(<DeliveryRights {...defaultProps} {...props} />);

    test('renders table for current vendor rules', () => {
        const { container } = renderWrapper();
        expect(container).toMatchSnapshot();
    });

    test('renders table for current subaccount rules', () => {
        const accountRules = {
            accountType: 'Subaccount',
            vendorRules,
            subaccountRules: [
                {
                    active: true,
                    policy: 'Monetize',
                    service: 'TikTok',
                    territory: 'US',
                    startDate: dayjs
                        .utc('2023-01-02T00:00:00.000Z')
                        .format('D MMM YYYY HH:mm:ss'),
                    endDate: '-',
                    createdDate: dayjs
                        .utc('2023-01-01T00:00:00.000Z')
                        .format('D MMM YYYY HH:mm:ss'),
                    lastModifiedDate: dayjs
                        .utc('2023-01-01T00:00:00.000Z')
                        .format('D MMM YYYY HH:mm:ss'),
                    createdBy: {
                        profileId: '123',
                        profileType: 'ContentProfile',
                        profileName: 'Name',
                    },
                    lastModifiedBy: {
                        profileId: '123',
                        profileType: 'ContentProfile',
                        profileName: 'Name',
                    },
                },
            ],
        } as AccountFingerprintRules;

        const { container } = renderWrapper({
            rules: accountRules,
        });
        expect(container).toMatchSnapshot();
    });

    test('renders table with vendor rules as default when no subaccount rules', () => {
        const accountRules = {
            accountType: 'Subaccount',
            vendorRules,
            subaccountRules,
        } as AccountFingerprintRules;

        const { container } = renderWrapper({
            rules: accountRules,
        });
        expect(container).toMatchSnapshot();
    });
});
