<?php

namespace Wetail\Shipping\Engine;

defined( __NAMESPACE__ . '\PATH' ) or die();

if ( class_exists( __NAMESPACE__ . '\Wetail_Shipping_Local_Shipping_Label' ) ) {
	return;
}

class Wetail_Shipping_Local_Shipping_Label{
	const TABLE_NAME = 'wetail_shipping_labels';

	/**
	 * @var $wc_order_id
	 */
	protected $wc_order_id;

	/**
	 * @var $date_created
	 */
	protected $date_created;

	/**
	 * @var $data
	 */
	protected $data;

	/**
	 * @var $id
	 */
	protected $id;

	public function __construct( $wc_order_id ) {
		$this->wc_order_id = $wc_order_id;
		$this->read();

	}

    /**
     * Sets vars from DB
     * @return string
     * @since 1.0.5
     */
    private function read(){
        global $wpdb;
	    $table_name = $wpdb->prefix . self::TABLE_NAME;
	    wetail_shipping_write_log("READING LABEL");
        $result = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$table_name} WHERE wc_order_id = %d ORDER BY date_created DESC LIMIT 1;", $this->wc_order_id ) );

	    wetail_shipping_write_log("Last query executed: " . $wpdb->last_query);
        if( ! empty( $result ) ){
			$this->id               = $result->id;
			$this->date_created     = $result->date_created;
			$this->data             = $result->data;
        }
    }

	/**
	 * @param $label_id
	 *
	 * @return bool
	 */
	private function read_by_id( $label_id ): bool {
		global $wpdb;
		$table_name = $wpdb->prefix . self::TABLE_NAME;
		wetail_shipping_write_log("READING LABEL BY ID");
		$result = $wpdb->get_row( $wpdb->prepare("SELECT * from {$table_name} WHERE wc_order_id = %d AND id = %d;", $this->wc_order_id, $label_id  ) );

		wetail_shipping_write_log("Last query executed: " . $wpdb->last_query);
		if( ! empty( $result ) ){
			$this->id               = $result->id;
			$this->date_created     = $result->date_created;
			$this->data             = $result->data;
			return true;
		}

		return false;
	}

	public function get_label_by_id( $label_id ): bool {
		return $this->read_by_id( $label_id );
	}

	/** Get date created
	 * @since 1.0.5
	 */
	public function get_date_created( ){
		return $this->date_created;
	}

	/** Get data
	 * @since 1.0.5
	 */
	public function get_data( ){
		return $this->data;
	}

	/**
	 * Retrieve all labels for the current order.
	 *
	 * @return array
	 * @since 1.0.5
	 */
	public function get_all_labels() {
		global $wpdb;

		$table_name = $wpdb->prefix . self::TABLE_NAME;
		$results = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT * FROM {$table_name} WHERE wc_order_id = %d ORDER BY date_created DESC;",
				$this->wc_order_id
			)
		);

		return $results ?: [];
	}
	/** Set data
	 * @param $data
	 * @since 0.9.1
	 */
	public function set_data( $data ){
		$this->data = $data;
	}

    /**
     * Saves object to db
     * @since 0.9.1
     */
    public function save(){
        global $wpdb;

		wetail_shipping_write_log("SAVING LABEL");
	    $data = array(
		    'wc_order_id'   => $this->wc_order_id,
		    'data'          => $this->data
	    );

		$format = array('%s', '%s'); // the format of each corresponding data value in the $data array, e.g., '%s' for string, '%d' for integer, '%f' for float.
		$wpdb->insert( $wpdb->prefix . self::TABLE_NAME, $data, $format );
		$this->id = $wpdb->insert_id;
		wetail_shipping_write_log("Last error: " . $wpdb->last_error);
    }
}

