<?php

namespace SGC\eGiftCard\Http\Model;

defined( 'ABSPATH' ) || exit; // Prevent direct file access.
/**
 * Class GiftCode
 *
 * Represents a gift code model for the eGift Card system.
 */
class GiftCode extends Model {
    
    /**
     * @var string The name of the database table associated with the model.
     */
    protected $tableName = 'gift_codes';

        /**
     * Retrieve records from the table.
     *
     * @param int|null $limit The number of records to retrieve. If null, retrieves all records.
     * @param int $offset The offset for pagination.
     * @return array An array of records.
     */
    public static function all( $limit = null, $offset = 0 )
    {
        $model = static::getInstance();

         // Base query
        $query = $model->_wpdb->prepare( "SELECT * FROM {$model->tableName} WHERE order_id != %d ORDER BY ID DESC", 0 );

        // Add LIMIT and OFFSET if $limit is not null
        if ( ! is_null( $limit ) ) {
            $query .= $model->_wpdb->prepare( " LIMIT %d OFFSET %d", $limit, $offset );
        }

        return $model->_wpdb->get_results( $query );
    }
}
