<?php

namespace RemoveUnusedMedia\DB;

const RESULT = 'rum_result';

function prep_cols($cols): string
{
    return is_array($cols) ? join(',', $cols) : $cols;
}

function prep_where($where): string
{
    if (is_array($where)) {
        $conditions = [];
        foreach ($where as $col => $val) {
            $conditions[] = "`$col` = $val";
        }
        $where = implode(" AND ", $conditions);
    }

    return $where;
}

function get_col($table, $col, $where): array
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $where      = prep_where($where);

    return $wpdb->get_col("SELECT $col FROM $table_name WHERE $where");
}

function get_row($table, $cols, $where): array
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $cols       = prep_cols($cols);
    $where      = prep_where($where);
    $result     = $wpdb->get_row("SELECT $cols FROM $table_name WHERE $where", ARRAY_A);

    return $result != null ? $result : ['error' => true];
}

function update($table, $data, $where): int
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $result     = $wpdb->update($table_name, $data, $where);

    return $result ?: 0;
}

function count($table, $where = ''): int
{
    global $wpdb;
    if (!table_exists($table)) {
        return 0;
    }
    $table_name = $wpdb->prefix . $table;
    if (!empty($where)) {
        $where = 'WHERE ' . prep_where($where);
    }
    $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name $where");

    return $count == null ? 0 : intval($count);
}

function count_wpml($table, $lang, $where = ''): int
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $wpml_table = $wpdb->prefix . 'icl_translations';
    if (!empty($where)) {
        $where = 'WHERE ' . prep_where($where);
    }
    $count =
        $wpdb->get_var(
            "SELECT COUNT(*) FROM $table_name a JOIN $wpml_table b ON a.id=b.element_id $where AND b.language_code='$lang' AND b.element_type='post_attachment'"
        );

    return $count == null ? 0 : intval($count);
}

function insert($table, $data): int
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $result     = $wpdb->insert($table_name, $data);

    return $result ?: 0;
}

function get($table, $cols, $where = ''): array
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $cols       = prep_cols($cols);
    if (!empty($where)) {
        $where = "WHERE " . prep_where($where);
    }

    $result = $wpdb->get_results(
        "SELECT $cols FROM $table_name $where",
        ARRAY_A
    );

    return $result != null ? $result : [];
}

function get_var($table, $col, $where): string
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $where      = prep_where($where);

    $result = $wpdb->get_var(
        "SELECT $col FROM $table_name WHERE $where",
        ARRAY_A
    );

    return $result != null ? $result : '';
}

function delete($table, $where): int
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;
    $result     = $wpdb->delete($table_name, $where);

    return $result ?: 0;
}

function table_exists($table): bool
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;

    return $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name;
}

function drop($table): bool
{
    global $wpdb;
    $table_name = $wpdb->prefix . $table;

    return $wpdb->query("DROP TABLE IF EXISTS $table_name");
}

function create_result_table(): array
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'rum_result';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
      id mediumint(9) NOT NULL,
      url varchar(200) NOT NULL,
      used_where varchar(1000),
      used_how varchar(1000),
      url_warning boolean,
      PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    return dbDelta($sql);
}
