const assert = require('node:assert/strict');
const sinon = require('sinon');
const getOgImage = require('../../../../core/frontend/meta/og-image');
const settingsCache = require('../../../../core/shared/settings-cache');

describe('getOgImage', function () {
    let localSettingsCache = {};

    beforeEach(function () {
        sinon.stub(settingsCache, 'get').callsFake(function (key) {
            return localSettingsCache[key];
        });
    });

    afterEach(function () {
        sinon.restore();
        localSettingsCache = {};
    });

    it('has correct fallbacks for context: home', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        assert(getOgImage({context: ['home'], home: {}}).endsWith('/content/images/settings-og.jpg'));

        localSettingsCache.og_image = '';

        assert(getOgImage({context: ['home'], home: {}}).endsWith('/content/images/settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['home'], home: {}}), null);
    });

    it('has correct fallbacks for context: post', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const post = {
            og_image: '/content/images/post-og.jpg',
            feature_image: '/content/images/post-feature.jpg'
        };

        assert(getOgImage({context: ['post'], post}).endsWith('post-og.jpg'));

        post.og_image = '';

        assert(getOgImage({context: ['post'], post}).endsWith('post-feature.jpg'));

        post.feature_image = '';

        assert(getOgImage({context: ['post'], post}).endsWith('settings-og.jpg'));

        localSettingsCache.og_image = '';

        assert(getOgImage({context: ['post'], post}).endsWith('settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['post'], post}), null);
    });

    it('has correct fallbacks for context: page', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const page = {
            og_image: '/content/images/page-og.jpg',
            feature_image: '/content/images/page-feature.jpg'
        };

        assert(getOgImage({context: ['page'], page}).endsWith('page-og.jpg'));

        page.og_image = '';

        assert(getOgImage({context: ['page'], page}).endsWith('page-feature.jpg'));

        page.feature_image = '';

        assert(getOgImage({context: ['page'], page}).endsWith('settings-og.jpg'));

        localSettingsCache.og_image = '';

        assert(getOgImage({context: ['page'], page}).endsWith('settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['page'], page}), null);
    });

    it('has correct fallbacks for context: page (legacy format)', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const post = {
            og_image: '/content/images/page-og.jpg',
            feature_image: '/content/images/page-feature.jpg'
        };

        assert(getOgImage({context: ['page'], post}).endsWith('page-og.jpg'));

        post.og_image = '';

        assert(getOgImage({context: ['page'], post}).endsWith('page-feature.jpg'));

        post.feature_image = '';

        assert(getOgImage({context: ['page'], post}).endsWith('settings-og.jpg'));

        localSettingsCache.og_image = '';

        assert(getOgImage({context: ['page'], post}).endsWith('settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['page'], post}), null);
    });

    it('has correct fallbacks for context: author', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const author = {
            cover_image: '/content/images/author-cover.jpg'
        };

        assert(getOgImage({context: ['author'], author}).endsWith('author-cover.jpg'));

        author.cover_image = '';

        assert.equal(getOgImage({context: ['author'], author}), null);
    });

    it('has correct fallbacks for context: author_paged', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const author = {
            cover_image: '/content/images/author-cover.jpg'
        };

        assert(getOgImage({context: ['author', 'paged'], author}).endsWith('author-cover.jpg'));

        author.cover_image = '';

        assert.equal(getOgImage({context: ['author', 'paged'], author}), null);
    });

    it('has correct fallbacks for context: tag', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const tag = {
            feature_image: '/content/images/tag-feature.jpg'
        };

        assert(getOgImage({context: ['tag'], tag}).endsWith('tag-feature.jpg'));

        tag.feature_image = '';

        assert(getOgImage({context: ['tag'], tag}).endsWith('settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['tag'], tag}), null);
    });

    it('has correct fallbacks for context: tag_paged', function () {
        localSettingsCache.og_image = '/content/images/settings-og.jpg';
        localSettingsCache.cover_image = '/content/images/settings-cover.jpg';

        const tag = {
            feature_image: '/content/images/tag-feature.jpg'
        };

        assert(getOgImage({context: ['tag', 'paged'], tag}).endsWith('tag-feature.jpg'));

        tag.feature_image = '';

        assert(getOgImage({context: ['tag', 'paged'], tag}).endsWith('settings-cover.jpg'));

        localSettingsCache.cover_image = '';

        assert.equal(getOgImage({context: ['tag', 'paged'], tag}), null);
    });

    describe('CDN image URLs', function () {
        it('returns CDN og_image for home context', function () {
            localSettingsCache.og_image = 'https://storage.ghost.is/c/6f/a3/site/content/images/2026/02/og.jpg';

            const result = getOgImage({context: ['home'], home: {}});
            assert(result.includes('storage.ghost.is'));
            assert(result.endsWith('/content/images/2026/02/og.jpg'));
        });

        it('returns CDN og_image for post context', function () {
            const post = {
                og_image: 'https://storage.ghost.is/c/6f/a3/site/content/images/2026/02/post-og.jpg',
                feature_image: '/content/images/post-feature.jpg'
            };

            const result = getOgImage({context: ['post'], post});
            assert(result.includes('storage.ghost.is'));
            assert(result.endsWith('post-og.jpg'));
        });

        it('falls back to CDN feature_image when post og_image is empty', function () {
            const post = {
                og_image: '',
                feature_image: 'https://storage.ghost.is/c/6f/a3/site/content/images/2026/02/feature.jpg'
            };

            const result = getOgImage({context: ['post'], post});
            assert(result.includes('storage.ghost.is'));
            assert(result.endsWith('feature.jpg'));
        });

        it('falls back to CDN cover_image for settings', function () {
            localSettingsCache.og_image = '';
            localSettingsCache.cover_image = 'https://storage.ghost.is/c/6f/a3/site/content/images/2026/02/cover.jpg';

            const post = {
                og_image: '',
                feature_image: ''
            };

            const result = getOgImage({context: ['post'], post});
            assert(result.includes('storage.ghost.is'));
            assert(result.endsWith('cover.jpg'));
        });
    });
});
