<?php

defined( 'ABSPATH' ) or exit;

class PWLPAR_Transaction {

    /* Properties */
    protected $id;
    protected $user_id;
    protected $points;
    protected $note;
    protected $earned_order_id;
    protected $redeemed_reward_id;
    protected $status;
    protected $create_date_gmt;
    protected $create_user_id;
    protected $update_date_gmt;
    protected $update_user_id;

    function __construct( $args = array() ) {

        $default_args = apply_filters( 'pwlpar_transaction_default_args', array(
            'pimwick_rewards_transaction_id'    => 0,
            'user_id'                           => false,
            'points'                            => 0,
            'note'                              => '',
            'earned_order_id'                   => 0,
            'redeemed_reward_id'                => 0,
            'status'                            => 'active',
            'create_date_gmt'                   => current_time( 'mysql', true ),
            'create_user_id'                    => get_current_user_id(),
            'update_date_gmt'                   => false,
            'update_user_id'                    => false,
        ) );

        $args = wp_parse_args( $args, $default_args );

        if ( isset( $args['id'] ) && empty( $args['pimwick_rewards_transaction_id'] ) ) {
            $args['pimwick_rewards_transaction_id'] = $args['id'];
        }

        $this->id                           = $args['pimwick_rewards_transaction_id'];
        $this->user_id                      = $args['user_id'];
        $this->points                       = $args['points'];
        $this->note                         = $args['note'];
        $this->earned_order_id              = $args['earned_order_id'];
        $this->redeemed_reward_id           = $args['redeemed_reward_id'];
        $this->status                       = $args['status'];
        $this->create_date_gmt              = $args['create_date_gmt'];
        $this->create_user_id               = $args['create_user_id'];
        $this->update_date_gmt              = $args['update_date_gmt'];
        $this->update_user_id               = $args['update_user_id'];
    }

    /* Getters */
    public function get_id( $context = 'view' ) {
        return $this->id;
    }

    public function get_user_id( $context = 'view' ) {
        return $this->user_id;
    }

    public function get_points( $context = 'view' ) {
        if ( $context == 'edit' ) {
            return $this->points;
        }

        return pwlpar_format_points( $this->points );
    }

    public function get_note( $context = 'view' ) {
        return $this->note;
    }

    public function get_earned_order_id( $context = 'view' ) {
        return $this->earned_order_id;
    }

    public function get_redeemed_reward_id( $context = 'view' ) {
        return $this->redeemed_reward_id;
    }

    public function get_status( $context = 'view' ) {
        if ( $context == 'edit' ) {
            return $this->status;
        }

        $statuses = pwlpar_get_transaction_statuses();
        if ( isset( $statuses[ $this->status ] ) ) {
            return $statuses[ $this->status ];
        } else {
            return $this->status;
        }
    }

    public function get_create_date( $context = 'view' ) {
        if ( $context == 'edit' ) {
            return $this->create_date_gmt;
        }

        return pwlpar_format_datetime( $this->create_date_gmt );
    }

    public function get_create_user_id( $context = 'view' ) {
        return $this->create_user_id;
    }

    public function get_update_date( $context = 'view' ) {
        if ( $context == 'edit' ) {
            return $this->update_date_gmt;
        }

        return pwlpar_format_datetime( $this->update_date_gmt );
    }

    public function get_update_user_id( $context = 'view' ) {
        return $this->update_user_id;
    }




    /* Setters */
    public function set_id( $id ) {
        $this->id = absint( $id );
    }

    public function set_user_id( $user_id ) {
        $this->user_id = absint( $user_id );
    }

    public function set_points( $points ) {
        $this->points = absint( $points );
    }

    public function set_note( $note ) {
        $this->note = sanitize_text_field( $note );
    }

    public function set_earned_order_id( $earned_order_id ) {
        $this->earned_order_id = absint( $earned_order_id );
    }

    public function set_redeemed_reward_id( $redeemed_reward_id ) {
        $this->redeemed_reward_id = absint( $redeemed_reward_id );
    }

    public function set_status( $status ) {
        $this->status = sanitize_text_field( $status );
    }

    public function set_create_date_gmt( $create_date_gmt ) {
        $this->create_date_gmt = pwlpar_get_gmt_from_date( $create_date_gmt );
    }

    public function set_create_user_id( $create_user_id ) {
        $this->create_user_id = absint( $create_user_id );
    }

    public function set_update_date_gmt( $update_date_gmt ) {
        $this->update_date_gmt = pwlpar_get_gmt_from_date( $update_date_gmt );
    }

    public function set_update_user_id( $update_user_id ) {
        $this->update_user_id = absint( $update_user_id );
    }




    /* Public methods */
    public function save() {
        global $wpdb, $pwlpardb;

        do_action( 'pwlpar_trasnaction_save', $this );

        $data = array(
            'pimwick_rewards_transaction_id' => $this->id,
            'user_id' => $this->user_id,
            'points' => $this->points,
            'note' => $this->note,
            'earned_order_id' => $this->earned_order_id,
            'redeemed_reward_id' => $this->redeemed_reward_id,
            'status' => $this->status,
        );

        if ( empty( $this->id ) ) {
            $data['create_date_gmt'] = current_time( 'mysql', true );
            $data['create_user_id'] = get_current_user_id();

            // Using a custom table, not a WordPress table, so this warning can be ignored.
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->insert( $pwlpardb->transaction, $data );
            if ( $wpdb->last_error ) {
                return false;
            }
            $this->set_id( $wpdb->insert_id );

        } else {
            $data['update_date_gmt'] = current_time( 'mysql', true );
            $data['update_user_id'] = get_current_user_id();

            // Using a custom table, not a WordPress table, so this warning can be ignored.
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $wpdb->update( $pwlpardb->transaction, $data, array( 'pimwick_rewards_transaction_id' => $this->id ) );
            if ( $wpdb->last_error ) {
                return false;
            }
        }

        do_action( 'pwlpar_transaction_saved', $this );

        return $this->id;
    }

    public function delete( $permanent = false ) {
        global $wpdb, $pwlpardb;

        do_action( 'pwlpar_transaction_delete', $this, $permanent );

        if ( empty( $this->id ) ) {
            return false;
        }

        if ( $permanent ) {
            // Using a custom table, not a WordPress table, so this warning can be ignored.
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $result = $wpdb->delete( $pwlpardb->transaction, array( 'pimwick_rewards_transaction_id' => $this->id ) );
        } else {
            $this->set_status( 'trash' );
            $this->save();
            $result = true;
        }

        do_action( 'pwlpar_transaction_deleted', $this, $permanent );

        return $result;
    }
}
