<?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/ChartOfAccounts.php';

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

$account = new ChartOfAccounts($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $account->id = $_GET['id'];
            $account->readOne();
            
            if($account->account_number != null) {
                $account_arr = array(
                    "id" => $account->id,
                    "account_number" => $account->account_number,
                    "account_name" => $account->account_name,
                    "account_type" => $account->account_type,
                    "parent_id" => $account->parent_id,
                    "description" => $account->description,
                    "opening_balance" => $account->opening_balance,
                    "current_balance" => $account->current_balance,
                    "vat_percentage" => $account->vat_percentage,
                    "is_active" => $account->is_active,
                    "created_at" => $account->created_at,
                    "updated_at" => $account->updated_at,
                    "account_type_name" => $account->getAccountTypeName()
                );
                
                http_response_code(200);
                echo json_encode($account_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Account not found."));
            }
        } else {
            $stmt = $account->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $accounts_arr = array();
                $accounts_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $account_item = array(
                        "id" => $id,
                        "account_number" => $account_number,
                        "account_name" => $account_name,
                        "account_type" => $account_type,
                        "parent_id" => $parent_id,
                        "description" => $description,
                        "opening_balance" => $opening_balance,
                        "current_balance" => $current_balance,
                        "vat_percentage" => $vat_percentage,
                        "is_active" => $is_active,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at,
                        "account_type_name" => $account->getAccountTypeName()
                    );
                    
                    array_push($accounts_arr["records"], $account_item);
                }
                
                http_response_code(200);
                echo json_encode($accounts_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->account_name)) {
            $account->account_number = $data->account_number ?? '';
            $account->account_name = $data->account_name;
            $account->account_type = $data->account_type ?? 'asset';
            $account->parent_id = $data->parent_id ?? null;
            $account->description = $data->description ?? '';
            $account->opening_balance = $data->opening_balance ?? 0;
            $account->current_balance = $data->current_balance ?? 0;
            $account->vat_percentage = $data->vat_percentage ?? 0;
            $account->is_active = $data->is_active ?? true;
            
            if($account->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Account was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create account."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create account. Account name is required."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->account_name)) {
            $account->id = $data->id;
            $account->account_number = $data->account_number ?? '';
            $account->account_name = $data->account_name;
            $account->account_type = $data->account_type ?? 'asset';
            $account->parent_id = $data->parent_id ?? null;
            $account->description = $data->description ?? '';
            $account->opening_balance = $data->opening_balance ?? 0;
            $account->current_balance = $data->current_balance ?? 0;
            $account->vat_percentage = $data->vat_percentage ?? 0;
            $account->is_active = $data->is_active ?? true;
            
            if($account->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Account was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update account."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update account. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $account->id = $_GET['id'];
            
            if($account->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Account was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete account."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete account. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
