<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Interfaces\JIT_Interface;
use Fab\View;
/**
 * Rating Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Rating_Notice extends Notice implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        $this->id = 'fab_rating_notice';
    }

    /**
     * Show admin rating notice.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // If rating is not considered, return.
        if ( ! get_option( 'fab_rating_notice_considered' ) ) {
            return;
        }

        // Check if the notice should be shown.
        if ( ! $this->should_show_notice() ) {
            return;
        }

        // Setup Buttons.
        $buttons   = array();
        $buttons[] = array(
            'message' => __( 'Rate Us 5 Stars →', 'floating-awesome-button' ),
            'url'     => 'https://wordpress.org/support/plugin/floating-awesome-button/reviews/#new-post',
            'target'  => '_blank',
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-primary-600 hover:bg-primary-700 transition-all font-semibold shadow-md animate__animated animate__pulse animate__infinite animate__slow',
        );
        $buttons[] = array(
            'message' => __( 'Maybe later', 'floating-awesome-button' ),
            'action'  => 'fab_maybe_later_rating_notice',
            'classes' => 'text-center text-xs px-2.5 py-1.5 bg-white hover:bg-red-600 border border-primary-600 hover:border-red-600 hover:text-red-600 hover:text-white transition-all font-semibold',
        );

        // Prepare the data for the admin notice.
        $data = array_merge(
            $this->get_default_data(),
            array(
                'title'   => __( '🌟 Rate Us 5 Stars!', 'floating-awesome-button' ),
                'message' => __( 'We would love to hear from you! Please take a moment to rate us 5 stars on WordPress.org. Your feedback is invaluable in helping us improve our plugin.', 'floating-awesome-button' ),
                'buttons' => $buttons,
            )
        );

        View::RenderStatic( 'Backend.admin-notice', $data );
    }

    /**
     * Handle notice action.
     *
     * @param WP_REST_Request $request The request object.
     * @return void
     */
    public function fab_handle_notice_action( $request ) {
        if ( 'fab_maybe_later_rating_notice' === $request->get_param( 'action' ) ) {
            // Dismiss notice.
            update_option( sprintf( 'fab_admin_notice_%s', $this->id ), true );
        }
    }

    /**
     * Consider showing rating notice after 30 days of FAB usage.
     *
     * @param int     $post_id The post ID.
     * @param WP_Post $post    The post object.
     * @param bool    $update  Whether the post is being updated.
     * @return void
     */
    public function fab_consider_showing_rating_notice( $post_id, $post, $update ) {
        // If post is trash, return.
        if ( 'trash' === $post->post_status ) {
            return;
        }

        // Check if post is published and been created more than 30 days ago.
        if ( 'publish' === $post->post_status && $post->post_date_gmt < strtotime( '-30 days' ) ) {
            update_option( 'fab_rating_notice_considered', true );
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Show admin usage tracking notice.
        add_action( 'admin_notices', array( __CLASS__, 'jit_fab_show_admin_notice' ) );

        // Handle notice action.
        add_action( 'fab_notice_action', array( __CLASS__, 'jit_fab_handle_notice_action' ) );

        // Consider showing rating notice after 30 days of FAB usage.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_fab_consider_showing_rating_notice' ), 10, 3 );
    }
}
