const express = require('express');
const router = express.Router();
const db = require('../config/db');

// 1. REGISTER NEW PATIENT
router.post('/patients', async (req, res) => {
    const { patientName, age, gender, phoneNumber } = req.body;

    if (!patientName || !age || !gender) {
        return res.status(400).json({ message: 'Required fields are missing' });
    }

    try {
        await db.query(
            'INSERT INTO patients (patientName, age, gender, phoneNumber) VALUES (?, ?, ?, ?)',
            [patientName, age, gender, phoneNumber]
        );
        return res.status(201).json({ message: 'Patient recorded successfully!' });
    } catch (err) {
        console.error(err);
        return res.status(500).json({ message: 'Internal server error' });
    }
});

// 2. GET ALL PATIENTS
router.get('/patients', async (req, res) => {
    try {
        const [rows] = await db.query('SELECT * FROM patients');
        return res.status(200).json(rows);
    } catch (err) {
        console.error(err);
        return res.status(500).json({ message: 'Internal server error' });
    }
});

module.exports = router;
