<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Average Posts Published
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Insight_Average_Posts_Published extends Insight_Type_Post implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
        $this->insight_id           = 'average_posts_published';
        $this->transient_name       = 'fab_insights_average_posts_published';
        $this->transient_expiration = 7 * 24 * HOUR_IN_SECONDS;
        $this->insight_schedule     = 'weekly';
    }

    /**
     * Generate insight.
     *
     * @return int
     */
    public function generate_insight() {
        // Calculate average number of posts published this month.
        $args          = array(
            'post_type'  => 'post',
            'date_query' => array(
                array(
                    'year' => gmdate( 'Y' ),
                ),
            ),
            'fields'     => 'ids',
        );
        $monthly_posts = new \WP_Query( $args );
        $data          = round( $monthly_posts->found_posts / 12, 2 );

        // Set the data to the cache.
        $this->set_transient( $data );

        // Return the data.
        return $data;
    }

    /**
     * Add the insight to the dashboard data.
     *
     * @param array $data The dashboard data.
     * @return array
     */
    public function add_insight_to_dashboard_data( $data ) {
        // If the insight type is not set, set it to an empty array.
        if ( ! isset( $data['labels'][ $this->insight_type ] ) ) {
            $data['labels'][ $this->insight_type ] = array();
        }

        // Add the insight to the dashboard data.
        $data['labels'][ $this->insight_type ][ $this->insight_id ] = __( 'Average Posts Published (This Month)', 'floating-awesome-button' );

        return $data;
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Load the insight.
        add_filter( 'fab_insights_dashboard', array( __CLASS__, 'jit_load_insight' ), 10, 2 );

        // Add the insight to the dashboard data.
        add_filter( 'fab_insights_dashboard_data', array( __CLASS__, 'jit_add_insight_to_dashboard_data' ), 10, 2 );

        // Schedule cron to regenerate insight.
        add_action( 'init', array( __CLASS__, 'jit_schedule_regenerate_insight' ) );
        add_action( 'fab_insights_average_posts_published_schedule_action', array( __CLASS__, 'jit_generate_insight' ) );
        add_action( 'fab_insights_average_posts_published_regenerate_insight', array( __CLASS__, 'jit_regenerate_insight' ) );
    }
}
