import { NextRequest, NextResponse } from 'next/server';
import { postgresClient } from '@/lib/services/postgres-client';

const KIOSK_KEYS = ['kiosk_company_category_ids', 'kiosk_excluded_company_ids'];
const MOBILE_KEYS = ['mobile_company_category_ids', 'mobile_excluded_company_ids'];
const ALL_KEYS = [...KIOSK_KEYS, ...MOBILE_KEYS];

function parseIds(value: string | null): number[] {
  if (!value) return [];
  return value.split(',').map((s: string) => parseInt(s.trim(), 10)).filter((n: number) => !isNaN(n));
}

export async function GET() {
  try {
    const result = await postgresClient.query(
      `SELECT setting_key, setting_value FROM kiosk_settings WHERE setting_key = ANY($1)`,
      [ALL_KEYS]
    );

    const map: Record<string, string> = {};
    result.rows.forEach((r: any) => { map[r.setting_key] = r.setting_value || ''; });

    return NextResponse.json({
      kiosk: {
        company_category_ids: parseIds(map['kiosk_company_category_ids'] ?? '1'),
        excluded_company_ids: parseIds(map['kiosk_excluded_company_ids'] ?? ''),
      },
      mobile: {
        company_category_ids: parseIds(map['mobile_company_category_ids'] ?? '1'),
        excluded_company_ids: parseIds(map['mobile_excluded_company_ids'] ?? ''),
      },
    });
  } catch (error) {
    console.error('Error fetching display settings:', error);
    return NextResponse.json({ error: 'Failed to fetch display settings' }, { status: 500 });
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { setting_key, setting_value } = body;

    if (!setting_key || !ALL_KEYS.includes(setting_key)) {
      return NextResponse.json({ error: 'Invalid setting_key' }, { status: 400 });
    }

    const valueToStore = Array.isArray(setting_value)
      ? setting_value.join(',')
      : String(setting_value ?? '');

    await postgresClient.query(
      `INSERT INTO kiosk_settings (setting_key, setting_value, updated_at)
       VALUES ($1, $2, CURRENT_TIMESTAMP)
       ON CONFLICT (setting_key)
       DO UPDATE SET setting_value = EXCLUDED.setting_value, updated_at = CURRENT_TIMESTAMP`,
      [setting_key, valueToStore]
    );

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('Error updating display settings:', error);
    return NextResponse.json({ error: 'Failed to update display settings' }, { status: 500 });
  }
}
