<?php
namespace WPVOFW;

use WPVOFW\Frontend as Module; 
use WPVOFW\Feedback;
use WPVOFW\Classes\ReviewReminder;

class Plugin
{
    private static $instance;

    public static function init()
    {
        if (null === self::$instance) {
            self::$instance = new Plugin();
        }
        return self::$instance;
    }
    // The Constructor.
    public function __construct()
    {
       
        if(file_exists(WPVOFW_PATH . 'includes/pro/init.php')){
            require_once WPVOFW_PATH . 'includes/pro/init.php';
        }
        
        new Module();

        add_action('add_meta_boxes', array($this,'custom_order_meta_box'),1,2);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);

        new ReviewReminder([
            'reminder_interval' => 30,
            'option_prefix' => 'wpvofw_',
            'display_condition_callback' => [ $this, 'allow_review_reminder' ],
            'notice_message' => "I hope you are enjoying our plugin <b>Order Feedback for WooCommerce</b>. If you find it useful, please consider leaving a review on <a href='https://wordpress.org/support/plugin/order-feedback-woo/reviews/' target='_blank'>WordPress.org</a>. Thanks!"
        ]);
    }
    public function enqueue_scripts(){
        //enqueue admin styles on order_feedback_woo pages and woocommcere order page
       
        $screen = get_current_screen();

        if(!in_array($screen->id, ['woocommerce_page_wc-orders', 'toplevel_page_wpvofw-feedback', 'order-feedback_page_wpvofw-settings'])){
            return;
        }

        if($screen->id == 'woocommerce_page_wc-orders'){
            if (!isset($_GET['action'])) {
               return;
            }
        }

        wp_enqueue_style('wpvofw-admin-style', WPVOFW_URL . 'assets/css/admin.css', [], WPVOFW_VERSION);

    }    


     function custom_order_meta_box($post_type, $post ){
        //check if the post type is wc-orders
        if($post_type !== 'woocommerce_page_wc-orders'){
            return;
        }
        //check if feedback from is submitted
        $order_id = $post->get_id();
        $order = wc_get_order($order_id);
        $form_submitted = $order->get_meta('feedback_form_submitted', true);
        if(!$form_submitted){
            return;
        }
        //add meta box
        add_meta_box(
            'wpvofw-feedback-data',      // Unique ID
            esc_html__( 'Order Feedback', 'order-feedback-woo' ),    // Title
            [$this,'render_feedback_data'],
            'woocommerce_page_wc-orders',         // Admin page (or post type)
            'side',         // Context
            // 'default'         // Priority
        );
    }
   
    function render_feedback_data($order){
        $order_id = $order->get_id();
        $feedback_data = Feedback::get_feedback_by_order_id($order_id);
        if($feedback_data){
            $title = isset($feedback_data->title) ? $feedback_data->title : '';
            $message = isset($feedback_data->message) ? $feedback_data->message : '';
            $rating = isset($feedback_data->rating) ? (int)$feedback_data->rating : '';
            $dateString = $feedback_data->date_created;
            $timestamp = strtotime($dateString);;
            $formattedDate = date('F j, Y \a\t g:i a',$timestamp );
            $ratingScale = isset($feedback_data->rating_scale) ? (int)$feedback_data->rating_scale : 5;
            $values = [
                'title' => $title,
                'message' => $message,
                'rating'    => $rating,
                'rating_scale' => $ratingScale,
                'date'  => $formattedDate

            ];
            $html = '';
            $html .= "<div class = 'wpvofw-feedback-data'>";
            if($title){
                $html .= "<span class = 'wpvofw-title'><b>Title:</b> {$title}</span>";
            }
            if($message){
                $html .= "<span class = 'wpvofw-message'><b>Message:</b> {$message}</span>";
            }
            if($rating){
                $html .= "<span class = 'wpvofw-rating'><b>Rating:</b> {$rating}</span>";
            }
            if($formattedDate){
                $html .= "<span class = 'wpvofw-feedback-date'><b>Added On:</b> {$formattedDate}</span>";
            }
            $html .= "</div>";

            // Apply filter and capture the modified HTML
            $html = apply_filters('wpvofw/order-edit/feedback', $html, $values);
            echo $html; // Return the modified HTML
        }
    }

    public function allow_review_reminder(){
        
        // Get the activation date of the plugin
        $activation_date = get_option( 'wpvofw_activation_date' );

        // If the activation date is not set, set it to the current time
        if ( ! $activation_date ) {
            update_option( 'wpvofw_activation_date', time() );
            return false; // Don't display the notice yet
        }

        // Calculate the number of days since activation
        $days_since_activation = ( time() - $activation_date ) / ( 24 * 3600 );

        // Display the notice only if it has been more than 7 days since activation
        return $days_since_activation >= 7;
    }
}
Plugin::init();
