<?php

namespace GriffinForms\Frontend\Sql;

use GriffinForms\Traits\Singleton;

class Format extends \GriffinForms\Frontend\Format
{   
    use Singleton;
    
    public function itemExists($item_type, $id)
    {
        $id = absint($id);
        $table = $this->config->getTable($item_type);
    
        if ($id < 1 || $table === false) {
            return false;
        }
    
        global $wpdb;
        $cache_key = 'item_exists_' . $item_type . '_' . $id;
        $cache_group = 'griffinforms';
    
        // Try to get the result from the cache
        $item_exists = wp_cache_get($cache_key, $cache_group);
    
        // If it's not cached, check the database
        if (false === $item_exists) {
            $result = $wpdb->get_row(
                $wpdb->prepare(
                    "SELECT id FROM %i WHERE id = %d",
                    $table, $id
                )
            );
    
            // Determine if the item exists
            $item_exists = $result !== null;
    
            // Cache the result
            wp_cache_set($cache_key, $item_exists, $cache_group, 86400);
        }
    
        return $item_exists;
    }

    public function getFieldType($field_id)
    {
        global $wpdb;
        $cache_key = 'field_type_' . $field_id;
        $cache_group = 'griffinforms';

        // Try to get the field type from the cache
        $field_type = wp_cache_get($cache_key, $cache_group);

        // If the field type is not cached, fetch it from the database
        if (false === $field_type) {
            $table = $this->config->getTable('field');

            // Fetch the field type from the database
            $field_type = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT field_type FROM %i WHERE id = %d",
                    $table, $field_id
                )
            );

            // Store the field type in the cache
            if ($field_type) {
                wp_cache_set($cache_key, $field_type, $cache_group, 86400);
            }
        }

        return $field_type;
    }

    function getItemById($id, $type)
    {
        global $wpdb;

        $table_name = $this->config->getTable($type);

        $result = $wpdb->get_row(
            $wpdb->prepare(
                "SELECT * FROM %i WHERE id = %d",
                $table_name,
                absint($id)
            ), ARRAY_A
        );

        // Return the result (will return null if no matching row is found)
        return $result;
    }

    /**
     * Fetch a single column value from a database table
     *
     * @param string $item_type Type of item (e.g., 'formtheme', 'form', 'field')
     * @param string $match_column Column to match against
     * @param mixed $match_value Value to match
     * @param string $value_column Column to retrieve
     *
     * @return mixed The value from $value_column or null if not found
     */
    public function getItemValue($item_type, $match_column, $match_value, $value_column)
    {
        global $wpdb;

        $table = $this->config->getTable($item_type);

        if ($table === false) {
            return null;
        }

        $result = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT %i FROM %i WHERE %i = %s",
                $value_column,
                $table,
                $match_column,
                $match_value
            )
        );

        return $result;
    }
}