<?php

defined( 'ABSPATH' ) or exit;

final class PWLPAR_DB {

    public $offer;
    public $offer_restriction;
    public $reward;
    public $transaction;

    function __construct() {
        $this->set_table_names();

        add_action( 'pwlpar_installed', array( $this, 'install_tables' ) );
        add_action( 'pwlpar_updated', array( $this, 'install_tables' ) );
    }

    function set_table_names( $allow_multisite = true ) {
        global $wpdb;

        $prefix = $wpdb->prefix;

        if ( $allow_multisite == true && get_option( 'pwlpar_setting_multisite_shared_database', apply_filters( 'pwlpar_default_setting_multisite_shared_database', 'no' ) ) == 'yes' ) {
            $prefix = $wpdb->base_prefix;
        }

        $this->offer                = $prefix . 'pimwick_rewards_offer';
        $this->offer_restriction    = $prefix . 'pimwick_rewards_offer_restriction';
        $this->reward               = $prefix . 'pimwick_rewards_reward';
        $this->transaction          = $prefix . 'pimwick_rewards_transaction';
    }

    function install_tables() {
        global $wpdb;

        // Switch to the local database in case we're multisite and have switched sites.
        $this->set_table_names( false );

        //
        // Creating custom tables for the plugin data.
        //
        // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
        $wpdb->query( $wpdb->prepare( "
            CREATE TABLE IF NOT EXISTS %i (
                `pimwick_rewards_offer_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `title` VARCHAR(255) NOT NULL,
                `description` TEXT NULL DEFAULT NULL,
                `coupon_amount` VARCHAR(25) NULL DEFAULT NULL,
                `points` INT UNSIGNED NULL DEFAULT NULL,
                `free_shipping` TINYINT(1) NOT NULL DEFAULT 0,
                `individual_use` TINYINT(1) NOT NULL DEFAULT 0,
                `exclude_sale_items` TINYINT(1) NOT NULL DEFAULT 0,
                `menu_order` INT UNSIGNED NOT NULL DEFAULT 0,
                `status` VARCHAR(20) NOT NULL DEFAULT 'draft',
                `create_date_gmt` DATETIME NOT NULL,
                `create_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `update_date_gmt` DATETIME NULL DEFAULT NULL,
                `update_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                PRIMARY KEY (`pimwick_rewards_offer_id`),
                INDEX `IX_status` (`status`)
            );
        ", $this->offer ) );
        if ( $wpdb->last_error != '' ) {
            wp_die( esc_html( $wpdb->last_error ) );
        }

        $wpdb->query( $wpdb->prepare( "
            CREATE TABLE IF NOT EXISTS %i (
                `pimwick_rewards_offer_id` INT UNSIGNED NOT NULL,
                `type` VARCHAR(25) NOT NULL,
                `object_id` BIGINT(20) UNSIGNED NOT NULL,
                PRIMARY KEY (`pimwick_rewards_offer_id`, `type`, `object_id`)
            );
        ", $this->offer_restriction ) );
        if ( $wpdb->last_error != '' ) {
            wp_die( esc_html( $wpdb->last_error ) );
        }

        $wpdb->query( $wpdb->prepare( "
            CREATE TABLE IF NOT EXISTS %i (
                `pimwick_rewards_reward_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `pimwick_rewards_offer_id` INT UNSIGNED NOT NULL,
                `user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `status` VARCHAR(20) NOT NULL DEFAULT 'pending',
                `create_date_gmt` DATETIME NOT NULL,
                `create_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `update_date_gmt` DATETIME NULL DEFAULT NULL,
                `update_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                PRIMARY KEY (`pimwick_rewards_reward_id`),
                INDEX `IX_user_id` (`user_id`),
                INDEX `IX_status` (`status`)
            );
        ", $this->reward ) );
        if ( $wpdb->last_error != '' ) {
            wp_die( esc_html( $wpdb->last_error ) );
        }

        $wpdb->query( $wpdb->prepare( "
            CREATE TABLE IF NOT EXISTS %i (
                `pimwick_rewards_transaction_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                `user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `points` INT NOT NULL DEFAULT 0,
                `note` TEXT NULL DEFAULT NULL,
                `earned_order_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `redeemed_reward_id` INT UNSIGNED NULL DEFAULT NULL,
                `status` VARCHAR(20) NOT NULL DEFAULT 'active',
                `create_date_gmt` DATETIME NOT NULL,
                `create_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                `update_date_gmt` DATETIME NULL DEFAULT NULL,
                `update_user_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
                PRIMARY KEY (`pimwick_rewards_transaction_id`),
                INDEX `IX_user_id` (`user_id`),
                INDEX `IX_earned_order_id` (`earned_order_id`),
                INDEX `IX_status` (`status`)
            );
        ", $this->transaction ) );
        if ( $wpdb->last_error != '' ) {
            wp_die( esc_html( $wpdb->last_error ) );
        }
        // phpcs:enable

        $this->set_table_names();
    }
}

return new PWLPAR_DB();
