<?php
require_once __DIR__ . '/../_pdo_db_connect.php';
require_once __DIR__ . '/../_response.php';
require_once __DIR__ . '/../_middleware/auth_check.php';
$payload = require_auth();
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) json_res(['error'=>'invalid_input'],400);
$pdo = get_db();
if (!empty($input['id'])){
    $stmt = $pdo->prepare('UPDATE sd_followups SET notes=?, scheduled_at=?, status=? WHERE id=?');
    $stmt->execute([$input['notes'],$input['scheduled_at'],$input['status'],$input['id']]);
    json_res(['status'=>'updated']);
}else{
    $stmt = $pdo->prepare('INSERT INTO sd_followups(customer_id,created_by,notes,scheduled_at,status,created_at) VALUES(?,?,?,?,?,NOW())');
    $stmt->execute([$input['customer_id'],$payload['sub'],$input['notes'],$input['scheduled_at'],$input['status']]);
    json_res(['status'=>'created','id'=>$pdo->lastInsertId()]);
}
