<?php
header("Content-Type: application/json");
require_once "../config.php";

$action = $_POST['action'] ?? $_GET['action'] ?? '';
$response = [];  // define it early

try {
    switch ($action) {
        // 👉 CREATE
        case 'insert':
            $authorID   = $_POST['authorId'] ?? '';
            $authorName = $_POST['authorName'] ?? '';
            if ($authorID && $authorName) {
                $sql = "INSERT INTO tblauthors (AuthorID, AuthorName)
                        VALUES (:aid, :aname)";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':aid', $authorID);
                $stmt->bindParam(':aname', $authorName);
                $stmt->execute();

                $response = ["status" => "success", "message" => "Author inserted"];
            } else {
                $response = ["status" => "error", "message" => "Missing data"];
            }
            break;

        // 👉 READ ALL
        case 'view':
            $stmt = $dbh->query("SELECT * FROM tblauthors ORDER BY id DESC");
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $response = ["status" => "success", "data" => $data];
            break;

        // 👉 UPDATE
        case 'update':
            $authorID   = $_POST['authorId'] ?? '';
            $authorName = $_POST['name'] ?? '';
            if ($authorID && $authorName) {
                $sql = "UPDATE tblauthors
                        SET AuthorName=:aname
                        WHERE AuthorID=:aid";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':aid', $authorID);
                $stmt->bindParam(':aname', $authorName);
                $stmt->execute();

                $response = ["status" => "success", "message" => "Author updated"];
            } else {
                $response = ["status" => "error", "message" => "Missing data"];
            }
            break;

        // 👉 DELETE
        case 'delete':
            $authorId = $_POST['authorId'] ?? '';
            if ($authorId) {
                $sql = "DELETE FROM tblauthors WHERE AuthorID = :authorId";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':authorId', $authorId, PDO::PARAM_STR); // use STR if AuthorID is varchar
                $stmt->execute();

                $response = ["status" => "success", "message" => "Author deleted"];
            } else {
                $response = ["status" => "error", "message" => "Missing id"];
            }
            break;

        // 🔹 Fetch all authors (default)
        default:
            $sql = "SELECT id, AuthorID, AuthorName, creationDate, UpdationDate
                    FROM tblauthors
                    ORDER BY AuthorName ASC";
            $stmt = $dbh->prepare($sql);
            $stmt->execute();
            $authors = $stmt->fetchAll(PDO::FETCH_ASSOC);

            $response = $authors
                ? ["status" => "success", "data" => $authors]
                : ["status" => "error", "message" => "No authors found"];
            break;
    }
} catch (Exception $e) {
    $response = ["status" => "error", "message" => $e->getMessage()];
}

echo json_encode($response);
exit;
