import type { Route } from '@/types';
import got from '@/utils/got';

import { renderQuestionDescription } from './templates/question-description';

const host = 'https://leetcode.com';

export const route: Route = {
    path: '/dailyquestion/en',
    radar: [
        {
            source: ['leetcode.com/'],
        },
    ],
    name: 'Unknown',
    maintainers: [],
    handler,
    url: 'leetcode.com/',
};

async function handler() {
    const question = {
        date: '',
        link: '',
        titleSlug: '',
        content: '',
        frontedId: '',
        difficulty: '',
        tags: '',
    };
    const url = host + '/graphql';
    const dailyQuestionPayload = {
        query: /* GraphQL */ `
            query questionOfToday {
                activeDailyCodingChallengeQuestion {
                    date
                    link
                    question {
                        frontendQuestionId: questionFrontendId
                        titleSlug
                    }
                }
            }
        `,
        variables: {},
    };
    const dailyQuestionResponse = await got({
        method: 'post',
        url,
        headers: {
            'content-type': 'application/json',
        },
        body: JSON.stringify(dailyQuestionPayload),
    });
    const data = dailyQuestionResponse.data.data.activeDailyCodingChallengeQuestion;
    question.date = data.date;
    question.link = host + data.link;
    question.titleSlug = data.question.titleSlug;

    const detailsPayload = {
        operationName: 'questionData',
        query: /* GraphQL */ `
            query questionData($titleSlug: String!) {
                question(titleSlug: $titleSlug) {
                    questionId
                    questionFrontendId
                    title
                    titleSlug
                    content
                    translatedTitle
                    translatedContent
                    difficulty
                    topicTags {
                        name
                        slug
                        translatedName
                        __typename
                    }
                    __typename
                }
            }
        `,
        variables: {
            titleSlug: question.titleSlug,
        },
    };
    const detailsResponse = await got({
        method: 'post',
        url,
        headers: {
            'content-type': 'application/json',
        },
        body: JSON.stringify(detailsPayload),
    });
    const emoji = {
        Medium: '🟡',
        Easy: '🟢',
        Hard: '🔴',
    };

    const details = detailsResponse.data.data.question;
    question.content = details.content;
    question.frontedId = details.questionFrontendId;
    question.difficulty = emoji[details.difficulty];

    let topicTags = details.topicTags;
    topicTags = topicTags.map((item) => {
        let slug = '#' + item.slug;
        slug = slug.replaceAll('-', '_');
        return slug;
    });
    question.tags = topicTags.join(' ');

    const rssData = {
        title: question.frontedId + '.' + question.titleSlug,
        description: renderQuestionDescription({
            question,
        }),
        link: question.link,
    };

    return {
        title: 'LeetCode Daily Question',
        link: 'https://leetcode.com',
        description: 'Leetcode Daily Question',
        item: [
            {
                title: rssData.title,
                description: rssData.description + question.content,
                link: rssData.link,
            },
        ],
    };
}
