<?php
/**
 * Request Item Model — CRUD over wpnest_rfw_request_items.
 *
 * @package WPNest_Returns_For_WooCommerce
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class WPNest_RFW_Request_Item_Model {

    /**
     * Insert N item rows linked to a request.
     *
     * @param int   $request_id
     * @param array $items  Each: [order_item_id, product_id, variation_id, qty, reason_code, reason_text]
     * @return int Number of inserted rows.
     */
    public static function create_many( $request_id, array $items ) {
        global $wpdb;
        $request_id = (int) $request_id;
        if ( $request_id <= 0 || empty( $items ) ) {
            return 0;
        }
        $inserted = 0;
        foreach ( $items as $item ) {
            $row = array(
                'request_id'    => $request_id,
                'order_item_id' => isset( $item['order_item_id'] ) ? (int) $item['order_item_id'] : 0,
                'product_id'    => isset( $item['product_id'] ) ? (int) $item['product_id'] : 0,
                'variation_id'  => isset( $item['variation_id'] ) ? (int) $item['variation_id'] : 0,
                'qty'           => isset( $item['qty'] ) ? max( 1, (int) $item['qty'] ) : 1,
                'reason_code'   => isset( $item['reason_code'] ) ? substr( (string) $item['reason_code'], 0, 40 ) : '',
                'reason_text'   => isset( $item['reason_text'] ) ? (string) $item['reason_text'] : null,
            );

            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table insert.
            $ok = $wpdb->insert(
                wpnest_rfw_request_items_table(),
                $row,
                array( '%d', '%d', '%d', '%d', '%d', '%s', '%s' )
            );
            if ( false !== $ok ) {
                $inserted++;
            }
        }
        return $inserted;
    }

    /**
     * @param int $request_id
     * @return array of objects
     */
    public static function get_for_request( $request_id ) {
        global $wpdb;
        $request_id = (int) $request_id;
        if ( $request_id <= 0 ) {
            return array();
        }
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table read.
        $rows = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM %i WHERE request_id = %d ORDER BY id ASC',
                wpnest_rfw_request_items_table(),
                $request_id
            )
        );
        return is_array( $rows ) ? $rows : array();
    }

    /**
     * @param int $request_id
     * @return int rows deleted
     */
    public static function delete_for_request( $request_id ) {
        global $wpdb;
        $request_id = (int) $request_id;
        if ( $request_id <= 0 ) {
            return 0;
        }
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table delete.
        $deleted = $wpdb->delete( wpnest_rfw_request_items_table(), array( 'request_id' => $request_id ), array( '%d' ) );
        return false === $deleted ? 0 : (int) $deleted;
    }

    public static function delete_all() {
        global $wpdb;
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup.
        return false !== $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', wpnest_rfw_request_items_table() ) );
    }
}
