import { test } from '@japa/runner';
import { ReferenceIdentifierTypes } from '#contracts/enums';
import vine from '@vinejs/vine';

// node ace test functional --groups "ReferenceValidation"
test.group('ReferenceValidation', () => {
    test('validate valid DOI', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'https://doi.org/10.24341/tethys.236',
            type: ReferenceIdentifierTypes.DOI,
        };

        try {
            const payload = await validator.validate(data);
            assert.deepEqual(payload, data);
        } catch {
            assert.isTrue(false);
        }
    });

    test('validate invalid DOI', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'https://doi.org/invalid-doi',
            type: ReferenceIdentifierTypes.DOI,
        };

        let payload = {};
        try {
            payload = await validator.validate(data);
        } catch {
            assert.notDeepEqual(payload, data);
        }
    });

    test('validate valid Handle', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: '20.5000/abc123',
            type: ReferenceIdentifierTypes.Handle,
        };

        try {
            const payload = await validator.validate(data);
            assert.deepEqual(payload, data);
        } catch {
            assert.isTrue(false);
        }
    });
    test('validate valid ISBN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            // reference: '978-3-85316-090-9',
            // reference: '9783853160909',
            // reference: '978-3-900312-64-0', // Geologische Karte der Republik Österreich 1 : 50.000
            reference: '3-90031-264-8', // Geologische Karte der Republik Österreich 1 : 50.000
            type: ReferenceIdentifierTypes.ISBN,
        };

        try {
            const payload = await validator.validate(data);
            assert.deepEqual(payload, data);
        } catch {
            assert.isTrue(false);
        }
    });

    test('validate invalid ISBN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'invalid-isbn',
            type: ReferenceIdentifierTypes.ISBN,
        };

        let payload = {};
        try {
            payload = await validator.validate(data);
        } catch {
            assert.notDeepEqual(payload, data);
        }
    });

    test('validate valid URN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'urn:isbn:0451450523',
            type: ReferenceIdentifierTypes.URN,
        };

        try {
            const payload = await validator.validate(data);
            assert.deepEqual(payload, data);
        } catch {
            assert.isTrue(false);
        }
    });

    test('validate invalid URN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'invalid-urn',
            type: ReferenceIdentifierTypes.URN,
        };

        let payload = {};
        try {
            payload = await validator.validate(data);
        } catch {
            assert.notDeepEqual(payload, data);
        }
    });

    test('validate valid ISSN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: '1234-567X',
            type: ReferenceIdentifierTypes.ISSN,
        };

        try {
            const payload = await validator.validate(data);
            assert.deepEqual(payload, data);
        } catch {
            assert.isTrue(false);
        }
    });

    test('validate invalid ISSN', async ({ assert }) => {
        const validator = vine.compile(
            vine.object({
                reference: vine.string().validateReference({ typeField: 'type' }),
                type: vine.enum(Object.values(ReferenceIdentifierTypes)),
            }),
        );

        const data = {
            reference: 'invalid-issn',
            type: ReferenceIdentifierTypes.ISSN,
        };

        let payload = {};
        try {
            payload = await validator.validate(data);
        } catch {
            assert.notDeepEqual(payload, data);
        }
    });
    // test('validate invalid Handle', async ({ assert }) => {
    //     const validator = vine.compile(
    //         vine.object({
    //             reference: vine.string().validateReference({ typeField: 'type' }),
    //             type: vine.enum(Object.values(ReferenceIdentifierTypes)),
    //         })
    //     );

    //     const data = {
    //         reference: 'invalid-handle',
    //         type: ReferenceIdentifierTypes.Handle,
    //     };

    //     const result = await validator.validate(data);
    //     assert.isFalse(result.valid);
    // });

    // Add more tests for other reference types as needed
});
