<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit(0);
}

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../models/AccountTransaction.php';

$database = new Database();
$db = $database->getConnection();

$accountTransaction = new AccountTransaction($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $accountTransaction->id = $_GET['id'];
            $accountTransaction->readOne();
            
            if($accountTransaction->journal_entry_id != null) {
                $transaction_arr = array(
                    "id" => $accountTransaction->id,
                    "journal_entry_id" => $accountTransaction->journal_entry_id,
                    "account_id" => $accountTransaction->account_id,
                    "debit_amount" => $accountTransaction->debit_amount,
                    "credit_amount" => $accountTransaction->credit_amount,
                    "description" => $accountTransaction->description,
                    "created_at" => $accountTransaction->created_at,
                    "updated_at" => $accountTransaction->updated_at,
                    "transaction_type" => $accountTransaction->getTransactionType(),
                    "amount" => $accountTransaction->getAmount()
                );
                
                http_response_code(200);
                echo json_encode($transaction_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Account transaction not found."));
            }
        } elseif(isset($_GET['journal_entry_id'])) {
            $stmt = $accountTransaction->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $transactions_arr = array();
                $transactions_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $transaction_item = array(
                        "id" => $id,
                        "journal_entry_id" => $journal_entry_id,
                        "account_id" => $account_id,
                        "debit_amount" => $debit_amount,
                        "credit_amount" => $credit_amount,
                        "description" => $description,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at,
                        "transaction_type" => $accountTransaction->getTransactionType(),
                        "amount" => $accountTransaction->getAmount(),
                        "account_name" => $account_name,
                        "account_type" => $account_type,
                        "entry_number" => $entry_number,
                        "entry_date" => $entry_date
                    );
                    
                    array_push($transactions_arr["records"], $transaction_item);
                }
                
                http_response_code(200);
                echo json_encode($transactions_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Missing journal_entry_id parameter."));
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->journal_entry_id) && !empty($data->account_id)) {
            $accountTransaction->journal_entry_id = $data->journal_entry_id;
            $accountTransaction->account_id = $data->account_id;
            $accountTransaction->debit_amount = $data->debit_amount ?? 0;
            $accountTransaction->credit_amount = $data->credit_amount ?? 0;
            $accountTransaction->description = $data->description ?? '';
            
            if($accountTransaction->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Account transaction was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create account transaction."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create account transaction. Data is incomplete."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->journal_entry_id) && !empty($data->account_id)) {
            $accountTransaction->id = $data->id;
            $accountTransaction->journal_entry_id = $data->journal_entry_id;
            $accountTransaction->account_id = $data->account_id;
            $accountTransaction->debit_amount = $data->debit_amount ?? 0;
            $accountTransaction->credit_amount = $data->credit_amount ?? 0;
            $accountTransaction->description = $data->description ?? '';
            
            if($accountTransaction->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Account transaction was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update account transaction."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update account transaction. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $accountTransaction->id = $_GET['id'];
            
            if($accountTransaction->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Account transaction was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete account transaction."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete account transaction. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
