import type { APIRoute } from 'astro';
import { supabase } from '../../../lib/supabase';

export const prerender = false;

export const GET: APIRoute = async ({ params }) => {
  const { id } = params;

  if (!id) {
    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Template ID is required' 
    }), { status: 400 });
  }

  try {
    const { data: template, error } = await supabase
      .from('custom_page_templates')
      .select('*')
      .eq('id', id)
      .single();

    if (error) {

      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Template not found' 
      }), { status: 404 });
    }

    return new Response(JSON.stringify({ 
      success: true, 
      template,
      pageData: template.page_data 
    }), { status: 200 });

  } catch (error) {

    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Internal server error' 
    }), { status: 500 });
  }
};

export const PUT: APIRoute = async ({ params, request }) => {
  const { id } = params;

  if (!id) {
    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Template ID is required' 
    }), { status: 400 });
  }

  try {
    const body = await request.json();
    const { name, description, page_data, custom_css, updated_by } = body;

    const updateData: any = { 
      updated_at: new Date().toISOString() 
    };

    if (name) updateData.name = name;
    if (description !== undefined) updateData.description = description;
    if (page_data) updateData.page_data = page_data;
    if (custom_css !== undefined) updateData.custom_css = custom_css;
    if (updated_by) updateData.updated_by = updated_by;

    const { data: template, error } = await supabase
      .from('custom_page_templates')
      .update(updateData)
      .eq('id', id)
      .select()
      .single();

    if (error) {

      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Failed to update template' 
      }), { status: 500 });
    }

    return new Response(JSON.stringify({ 
      success: true, 
      template 
    }), { status: 200 });

  } catch (error) {

    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Internal server error' 
    }), { status: 500 });
  }
};

export const DELETE: APIRoute = async ({ params }) => {
  const { id } = params;

  if (!id) {
    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Template ID is required' 
    }), { status: 400 });
  }

  try {
    // Soft delete by setting is_active to false
    const { error } = await supabase
      .from('custom_page_templates')
      .update({ is_active: false })
      .eq('id', id);

    if (error) {

      return new Response(JSON.stringify({ 
        success: false, 
        error: 'Failed to delete template' 
      }), { status: 500 });
    }

    return new Response(JSON.stringify({ 
      success: true 
    }), { status: 200 });

  } catch (error) {

    return new Response(JSON.stringify({ 
      success: false, 
      error: 'Internal server error' 
    }), { status: 500 });
  }
};