import { NextRequest, NextResponse } from "next/server";
import { settingsStore, AppSettings } from "@/lib/settings";

export async function GET() {
  try {
    const settings = await settingsStore.get();
    return NextResponse.json(settings);
  } catch (error) {
    console.error("Error getting settings:", error);
    return NextResponse.json({ error: "Failed to get settings" }, { status: 500 });
  }
}

export async function PUT(request: NextRequest) {
  try {
    const body = await request.json() as Partial<AppSettings>;
    const settings = await settingsStore.set(body);
    return NextResponse.json(settings);
  } catch (error) {
    console.error("Error updating settings:", error);
    return NextResponse.json({ error: "Failed to update settings" }, { status: 500 });
  }
}

export async function PATCH(request: NextRequest) {
  try {
    const body = await request.json() as Partial<AppSettings>;
    const settings = await settingsStore.set(body);
    return NextResponse.json(settings);
  } catch (error) {
    console.error("Error patching settings:", error);
    return NextResponse.json({ error: "Failed to patch settings" }, { status: 500 });
  }
}
