DEFINE FUNCTION OVERWRITE fn::vm_price_per_minute(
	$vm_id: record
) {
    LET $vm = (select * from $vm_id)[0];
    LET $ip_price = IF $vm.public_ipv4.len() > 0 { 10 } ELSE { 0 };
    RETURN (
      ($vm.vcpus * 10) + (($vm.memory_mb + 256) / 200) + ($vm.disk_size_gb / 10) + $ip_price)
    * $vm.price_per_unit;
};

DEFINE FUNCTION OVERWRITE fn::delete_vm(
	$vm_id: record
) {
    LET $vm = (select * from $vm_id)[0];
    LET $account = $vm.in;
    LET $deleted_vm = $vm.patch([{
		'op': 'replace',
		'path': 'id',
		'value': type::record("deleted_vm:" + record::id($vm.id))
    }]);
    IF $vm.locked_nano >= 0 {
        UPDATE $account SET balance += $vm.locked_nano;
    };
    INSERT RELATION INTO deleted_vm ( $deleted_vm );
    RETURN DELETE $vm.id RETURN BEFORE;
};

DEFINE FUNCTION OVERWRITE fn::app_price_per_minute(
	$app_id: record
) {
    LET $app = (select * from $app_id)[0];
    RETURN 
     (($app.vcpus * 5) + 
        ($app.memory_mb / 200) + 
        ($app.disk_size_gb / 10))
    * $app.price_per_unit;
};

DEFINE FUNCTION OVERWRITE fn::delete_app(
	$app_id: record
) {
    LET $app = (select * from $app_id)[0];
    LET $account = $app.in;
    LET $deleted_app = $app.patch([{
		'op': 'replace',
		'path': 'id',
		'value': type::record("deleted_app:" + record::id($app.id))
    }]);
    IF $app.locked_nano >= 0 {
        UPDATE $account SET balance += $app.locked_nano;
    };
    INSERT RELATION INTO deleted_app ( $deleted_app );
    RETURN DELETE $app.id RETURN BEFORE;
};