/**
 * Gitea Repos Data
 * Fetches public repositories from Gitea org API
 */

import { cachedFetch } from "../lib/data-fetch.js";

const GITEA_URL = process.env.GITEA_INTERNAL_URL || process.env.GITEA_URL || "https://gitea.giersig.eu";
const GITEA_ORG = process.env.GITEA_ORG || "giersig.eu";

export default async function () {
  try {
    const url = `${GITEA_URL}/api/v1/orgs/${GITEA_ORG}/repos?limit=10&sort=newest`;

    const repos = await cachedFetch(url, {
      duration: "1h",
      type: "json",
    });

    return repos
      .filter((repo) => !repo.fork && !repo.private)
      .map((repo) => ({
        name: repo.name,
        full_name: repo.full_name,
        description: repo.description,
        html_url: repo.html_url,
        homepage: repo.website || repo.homepage,
        language: repo.language,
        stargazers_count: repo.stars_count || repo.stargazers_count || 0,
        forks_count: repo.forks_count || 0,
        open_issues_count: repo.open_issues_count || 0,
        topics: repo.topics || [],
        updated_at: repo.updated,
        created_at: repo.created,
      }))
      .slice(0, 10);
  } catch (error) {
    console.error("Error fetching Gitea repos:", error.message);
    return [];
  }
}
