import type { APIRoute } from 'astro';

export const GET: APIRoute = async () => {
  const WAKATIME_API_KEY = import.meta.env.WAKATIME_API_KEY;

  if (!WAKATIME_API_KEY) {
    return new Response(
      JSON.stringify({ error: "WAKATIME_API_KEY not configured" }),
      { status: 503, headers: { "Content-Type": "application/json" } }
    );
  }

  try {
    const response = await fetch(
      'https://wakatime.com/api/v1/users/current/summaries?range=last_6_months', {
        headers: {
          'Authorization': `Basic ${Buffer.from(WAKATIME_API_KEY).toString('base64')}`
        }
      }
    );

    const data = await response.json();
    return new Response(
      JSON.stringify({ data: data.data }),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );
  } catch (error) {
    console.error('API Error:', error);
    return new Response(
      JSON.stringify({ error: 'Failed to fetch WakaTime data' }),
      { status: 500 }
    );
  }
}
