/**
 *  Copyright (c) 2025 taskylizard. Apache License 2.0.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
const files = (
  [
    'adblockvpnguide.md',
    'ai.md',
    'android-iosguide.md',
    'audiopiracyguide.md',
    'beginners-guide.md',
    'devtools.md',
    'downloadpiracyguide.md',
    'edupiracyguide.md',
    'file-tools.md',
    'gaming-tools.md',
    'gamingpiracyguide.md',
    'img-tools.md',
    'internet-tools.md',
    'linuxguide.md',
    'miscguide.md',
    'non-english.md',
    'readingpiracyguide.md',
    'social-media-tools.md',
    'storage.md',
    'system-tools.md',
    'text-tools.md',
    'torrentpiracyguide.md',
    'unsafesites.md',
    'video-tools.md',
    'videopiracyguide.md'
  ] as const
).map((file) => ({
  name: file,
  url: `https://raw.githubusercontent.com/fmhy/edit/main/docs/${file}`
}))

export default defineCachedEventHandler(
  async (event) => {
    let body = '<!-- This is autogenerated content, do not edit manually. -->\n'

    const contents = await Promise.all(
      files.map(async (file) => {
        const content = await $fetch<string>(file.url)

        return content
      })
    )
    body += contents.join('\n\n')

    appendResponseHeaders(event, {
      'content-type': 'text/markdown;charset=utf-8',
      'cache-control': 'public, max-age=7200'
    })
    return body
  },
  {
    maxAge: 60 * 60,
    name: 'single-page',
    getKey: () => 'default' /* Can be extended in the future */
  }
)
