import { NextRequest, NextResponse } from 'next/server'
import { settingsManager } from '../../../lib/settings'

export async function GET() {
  try {
    const settings = await settingsManager.loadSettings()
    return NextResponse.json(settings)
  } catch (e: any) {
    return NextResponse.json({ error: e.message }, { status: 500 })
  }
}

export async function POST(req: NextRequest) {
  try {
    const updates = await req.json()
    const settings = await settingsManager.updateSettings(updates)
    return NextResponse.json(settings)
  } catch (e: any) {
    return NextResponse.json({ error: e.message }, { status: 500 })
  }
}

export async function PUT(req: NextRequest) {
  try {
    const { symbol, timeframe, layouts } = await req.json()
    
    if (symbol) await settingsManager.setSymbol(symbol)
    if (timeframe) await settingsManager.setTimeframe(timeframe)
    if (layouts) await settingsManager.setLayouts(layouts)
    
    const settings = await settingsManager.loadSettings()
    return NextResponse.json(settings)
  } catch (e: any) {
    return NextResponse.json({ error: e.message }, { status: 500 })
  }
}
