/* eslint-disable -- temporary disable for development/testing purposes */

import { NextRequest, NextResponse } from 'next/server';
import { getDBConnection } from '@/lib/db'; // adjust path as needed


// Define the type for a medical chart row
type MedicalChart = {
    id: number;
    user_id: string;
    manager_id: string;
    company_id: string;
    medical_history: string;
    diagnosis: string;
    immunizations: string;
    medications: string;
    precautions: string;
    date: string;
  };
  
  export async function GET(request: NextRequest) {
    const userId = request.nextUrl.searchParams.get('userId');
    const companyId = request.nextUrl.searchParams.get('companyId');
  
    if (!userId || !companyId) {
      return NextResponse.json({ error: 'Missing parameters' }, { status: 400 });
    }
  
    const connection = await getDBConnection();
  
    try {
      const [rows] = await connection.execute(
        'SELECT * FROM medical_chart WHERE user_id = ? AND company_id = ? LIMIT 1',
        [userId, companyId]
      );
  
      const chartRows = rows as MedicalChart[];
  
      if (chartRows.length === 0) {
        return NextResponse.json({ chart: null });
      }
  
      return NextResponse.json({ chart: chartRows[0] });
    } catch (err) {
      // console.error(err);
      return NextResponse.json({ error: 'Database query failed' }, { status: 500 });
    } finally {
      await connection.end();
    }
  }
// POST: Create or Update Medical Chart
export async function POST(request: NextRequest) {
  const body = await request.json();

  const {
    user_id,
    manager_id,
    company_id,
    medical_history,
    diagnosis,
    immunizations,
    medications,
    precautions,
  } = body;

  if (!user_id || !manager_id || !company_id) {
    return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
  }

  const connection = await getDBConnection();

  try {
    // Check if chart exists
    const [existing] = await connection.execute(
      'SELECT id FROM medical_chart WHERE user_id = ? AND company_id = ?',
      [user_id, company_id]
    );

    if ((existing as any[]).length > 0) {
      // Update
      await connection.execute(
        `UPDATE medical_chart SET 
          manager_id = ?, 
          medical_history = ?, 
          diagnosis = ?, 
          immunizations = ?, 
          medications = ?, 
          precautions = ?, 
          date = CURRENT_TIMESTAMP
        WHERE user_id = ? AND company_id = ?`,
        [
          manager_id,
          medical_history,
          diagnosis,
          immunizations,
          medications,
          precautions,
          user_id,
          company_id,
        ]
      );
    } else {
      // Insert
      await connection.execute(
        `INSERT INTO medical_chart 
        (user_id, manager_id, company_id, medical_history, diagnosis, immunizations, medications, precautions, date)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
        [
          user_id,
          manager_id,
          company_id,
          medical_history,
          diagnosis,
          immunizations,
          medications,
          precautions,
        ]
      );
    }

    return NextResponse.json({ success: true });
  } catch (err) {
    // console.error(err);
    return NextResponse.json({ error: 'Database operation failed' }, { status: 500 });
  } finally {
    await connection.end();
  }
}
