<?php
// employee_details.php

// Include the reusable CORS handler
require_once __DIR__ . '/cors.php';


$data = json_decode(file_get_contents('php://input'), true);
file_put_contents('debug.log', print_r($data, true)); // writes to a debug file


header('Content-Type: application/json');
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/PdoCrud.php';

$crud = new PdoCrud($pdo, 'employee_details', 'ID');

switch ($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        try {
            if (isset($_GET['id'])) {
                // Single employee request
                $employee = $crud->read($_GET['id']);
                if (!$employee) {
                    http_response_code(404);
                    echo json_encode(['error' => 'Employee not found']);
                    break;
                }
                echo json_encode($employee);
            } else {
                // List request with filters
                $filters = [];
                
                // Add status filter if provided
                if (isset($_GET['status']) && $_GET['status'] === 'active') {
                    $filters['status'] = 'active';
                }
                
                // You can add more filters here as needed
                // if (isset($_GET['department'])) {
                //     $filters['department'] = $_GET['department'];
                // }
                
                // Get results with pagination (default 100 records)
                $employees = $crud->list($filters);
                
                echo json_encode($employees);
            }
        } catch (Exception $e) {
            http_response_code(500);
            echo json_encode(['error' => 'Server error: ' . $e->getMessage()]);
        }
        break;

    case 'POST':
        try {
            $data = json_decode(file_get_contents('php://input'), true);
            if (!$data) {
                throw new Exception("Invalid input.");
            }
            $id = $crud->create($data);
            echo json_encode(['id' => $id]);
        } catch (Exception $e) {
            http_response_code(400);
            echo json_encode(['error' => $e->getMessage()]);
        }
        break;

        case 'PUT':
            $data = json_decode(file_get_contents("php://input"), true);
            $id = $data['id'] ?? null;
            if ($id) {
                unset($data['id']);
                $crud->update($id, $data);
                echo json_encode(['status' => 'updated']);
            } else {
                http_response_code(400);
                echo json_encode(['error' => 'Missing id']);
            }
            break;

    case 'DELETE':
        parse_str(file_get_contents("php://input"), $_DELETE);
        $id = $_DELETE['id'] ?? null;
        if ($id) {
            $crud->delete($id);
            echo json_encode(['status' => 'deleted']);
        } else {
            http_response_code(400);
            echo json_encode(['error' => 'Missing id']);
        }
        break;

    default:
        http_response_code(405);
        echo json_encode(['error' => 'Method not allowed']);
}
