<?php

/**
* Get all entity
*
* @param $args array
*
* @return array
*/
function esi_get_all_entity( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'number'     => 20,
        'offset'     => 0,
        'orderby'    => 'id',
        'order'      => 'ASC',
    );

    $args      = wp_parse_args( $args, $defaults );
    $cache_key = 'entity-all';
    $items     = wp_cache_get( $cache_key, 'esi' );

    if ( false === $items ) {
        $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_entity ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );

        wp_cache_set( $cache_key, $items, 'esi' );
    }

    return $items;
}

/**
* Fetch all entity from database
*
* @return array
*/
function esi_get_entity_count() {
    global $wpdb;

    return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'esi_entity' );
}

/**
* Fetch a single entity from database
*
* @param int   $id
*
* @return array
*/
function esi_get_entity( $id = 0 ) {
    global $wpdb;

    return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'esi_entity WHERE id = %d', $id ) );
}

/**
* Fetch a single entity from database
*
* @param int   $id
*
* @return array
*/
function esi_get_all_countries() {
    global $wpdb;

    $defaults = array(
        'orderby'    => 'country_name',
        'order'      => 'ASC',
    );

    $args      = wp_parse_args( $args, $defaults );
    $cache_key = 'region-all';
    $items     = wp_cache_get( $cache_key, 'esi-countries' );

    if ( false === $items ) {
        $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_country ORDER BY ' . $args['orderby'] .' ' . $args['order'] );

        wp_cache_set( $cache_key, $items, 'esi' );
    }

    return $items;
}

function esi_get_all_entity_type_id() {
    global $wpdb;

    $defaults = array(
        'orderby'    => 'id',
        'order'      => 'ASC',
    );

    $args      = wp_parse_args( $args, $defaults );
    $cache_key = 'entity-type-all';
    $items     = wp_cache_get( $cache_key, 'esi' );

    if ( false === $items ) {
        $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_entity_type ORDER BY ' . $args['orderby'] .' ' . $args['order'] );

        wp_cache_set( $cache_key, $items, 'esi' );
    }

    return $items;

}

/**
* Insert a entity entity
*
* @param array $args
*/
function esi_insert_entity( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'id'         => null,
        'name' => '',
        'description' => '',
        'url' => '',
        'is_investor' => '',
    );

    $args       = wp_parse_args( $args, $defaults );
    $table_name = $wpdb->prefix . 'esi_entity';

    // some basic validation
    if ( empty( $args['name'] ) ) {
        return new WP_Error( 'no-name', __( 'No Name provided.', 'esi' ) );
    }
    if ( empty( $args['description'] ) ) {
        return new WP_Error( 'no-description', __( 'No Description provided.', 'esi' ) );
    }
    if ( empty( $args['url'] ) ) {
        return new WP_Error( 'no-url', __( 'No URL provided.', 'esi' ) );
    }

    // remove row id to determine if entity or update
    $row_id = (int) $args['id'];
    unset( $args['id'] );

    if ( ! $row_id ) {
        // insert a entity
        if ( $wpdb->insert( $table_name, $args ) ) {
            return $wpdb->insert_id;
        }

    } else {

        // do update method here
        if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
            return $row_id;
        }
    }

    return false;
}

/**
* Deletitem
*
* @param int   $id
*
*/
function esi_delete_entity( $id = 0 ) {
    global $wpdb;
    $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'esi_entity WHERE id = %d', $id ) );
}

function sample_admin_notice_success() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}